In C, I can autoconnect signals with this code:
gtk_builder_connect_signals (builder, NULL)
How to do this in C++ with GTKmm?
You cannot use Glade to connect your signals when using gtkmm, you need to do that manually.
Glib::RefPtr builder = Gtk::Builder::create_from_file("glade_file.ui");
Gtk::Window *window1 = 0;
builder->get_widget("window1", window1);
Gtk::Button *button1 = 0;
builder->get_widget("button1", button1);
// get other widgets
...
button1->signal_clicked().connect(sigc::mem_fun(*this, &button1_clicked));
Have a look at these answers :
https://stackoverflow.com/a/3191472/1673000
https://stackoverflow.com/a/1637058/1673000
Of course you can, there is nothing wrong with mixing C and C++ code.
here is an example code that assumes the signal handler onComboBoxSelectedItemChange is set from glade on a GtkComboBox.
#include <gtkmm.h>
#include <string>
namespace GUI{
int init(){
auto app = Gtk::Application::create();
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("./res/GUI.glade");
gtk_builder_connect_signals(builder->gobj(), NULL);
Gtk::Window* mainWindow = nullptr;
builder->get_widget("mainWindow", mainWindow);
return app->run(*mainWindow);
}
extern "C"
void onComboBoxSelectedItemChange(GtkComboBox *widget, gpointer user_data){
int selectedIndex = gtk_combo_box_get_active(widget);
Gtk::MessageDialog dialog(std::to_string(selectedIndex).c_str());
dialog.run();
}
}
int main(){
return GUI::init();
}
you can build using
g++ -rdynamic -std=c++11 test.cpp $(pkg-config --cflags --libs gtkmm-3.0)
Related
Is there a GTK3 function that detects if a window has focus? I am currently using the following code:
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
GtkWidget *LinuxWindow;
static void buttonMessage(GtkWidget *widget, gpointer data)
{
g_print("Yay, you clicked me!\n");
}
int main() {
GtkWidget *Box, *Button;
int argC = 0;
char **argV;
// Setup the window and fixed grid
gtk_init(&argC, &argV);
LinuxWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
Box = gtk_fixed_new();
// Set the title
gtk_window_set_title(GTK_WINDOW(LinuxWindow), "Title");
// Setup the window events
gtk_widget_show_all(LinuxWindow);
g_signal_connect(G_OBJECT(LinuxWindow), "destroy", G_CALLBACK(gtk_main_quit),
NULL);
// Add controls
Button = gtk_button_new_with_label("Click Me!");
g_signal_connect(Button, "clicked", G_CALLBACK(buttonMessage), NULL);
gtk_fixed_put(GTK_FIXED(Box), Button, 20, 20);
gtk_fixed_move(GTK_FIXED(Box), Button, 20, 20);
gtk_widget_set_size_request(Button, 30, 100);
gtk_container_add(GTK_CONTAINER(LinuxWindow), Box);
gtk_widget_show_all(LinuxWindow);
// Create a dialog
GtkWidget *dialog;
dialog = gtk_message_dialog_new(
GTK_WINDOW(LinuxWindow), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Hello and welcome to my GTK GUI...", NULL);
gint ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
printf("%i", ret);
// Add the fixed grid and go the to the main window loop
gtk_main();
return 0;
}
I am compiling it using
g++ -std=c++17 -m64 -o gtkTest myGtkApp.cpp -lX11 `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0`
I want to detect if the window is focused and print it to the console.
There is a function: gtk_widget_is_focus ().
You need to assure parents have the "has-focus" property set.
Doc. excerpt:
gtk_widget_is_focus (GtkWidget *widget);
Determines if the widget is the focus widget within its toplevel. (This does not mean that the “has-focus” property is necessarily set; “has-focus” will only be set if the toplevel widget additionally has the global input focus.)
If you want to receive an event when the window is focused then register a callback for the enter-notify-event (signal)
In the linked doc. there is a section just three points after enter-notify-event that is what you want:
The “focus” signal
Sorry, I should have to mention this event the first time.
How to capture the global keys in Ubuntu in Qt application. I need to handle the keys like Ctrl, Shift even my qt application is minimized state also. Looks like LibQxt supports this. But as per my understanding this library won't have any support from Qt4. I am using Qt5.7. Do we have any other way to do this ?
This can be achieved using x11/xcb. The idea is to listen to a specific keyboard shortcut, system-wide, using the XGrabKey function from xlib, then catching the corresponding xcb event in the overridden nativeEventFilter method of a QAbstractNativeEventFilter subclass.
As an example, let's activate an application minimized window, using the Ctrl-A shortcut from anywhere.
The project must reference the x11extra qt module, and link the x11 library:
QT += x11extras
LIBS += -lX11
This is the filter header:
#include <QAbstractNativeEventFilter>
#include <QWidget>
class EventFilter : public QAbstractNativeEventFilter
{
public:
void setup(QWidget *target);
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result);
private:
int keycode;
QWidget * target;
};
and this is the implementation:
#include <xcb/xcb.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <QtX11Extras/QX11Info>
void EventFilter::setup(QWidget *target)
{
this->target = target;
Display * display = QX11Info::display();
unsigned int modifiers = ControlMask;
keycode = XKeysymToKeycode(display, XK_A);
XGrabKey(display, keycode, modifiers, DefaultRootWindow(display), 1, GrabModeAsync, GrabModeAsync);
}
bool EventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *)
{
if (eventType == "xcb_generic_event_t")
{
xcb_generic_event_t* xcbevent = static_cast<xcb_generic_event_t *>(message);
switch(xcbevent->response_type)
{
case XCB_KEY_PRESS:
xcb_key_press_event_t * keypress_event = static_cast<xcb_key_press_event_t *>(message);
if(keypress_event->state & XCB_MOD_MASK_CONTROL)
{
if(keypress_event->detail == keycode)
{
qDebug() << "ACTIVATING ...";
target->activateWindow();
}
}
}
}
return false;
}
In a main, we create a widget on the fly, instantiate, install and setup our filter.
#include "eventfilter.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.setGeometry(100, 100, 400, 300);
w.show();
EventFilter filter;
a.installNativeEventFilter(&filter);
filter.setup(&w);
return a.exec();
}
If the user minimize the widget, or the widget is deactivated, the Ctrl-A shortcut should prompt it back to the foreground.
Please notice that the XGrabKey call from the setup method could fail if some other x client grabbed the same key combination, already.
Also notice that extra modifier masks will be delivered in the event state field if some lock keys are on (e.g. on my system the pressed caps lock yelds an extra XCB_MOD_MASK_LOCK, and the num lock an extra XCB_MOD_MASK_2).
I'm rather new to programming and especially when it comes to how to including libraries and alike activities. I've been programming a bit using Python in the past and I've been using GTK to create windows, something I've intended to do when programming with c++ as well. To get things started, here's my code:
#include <stdlib.h>
#include <gtk/gtk.h>
static void helloWorld (GtkWidget *wid, GtkWidget *win)
{
GtkWidget *dialog = NULL;
dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
int main (int argc, char *argv[])
{
GtkWidget *button = NULL;
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;
/* Initialize GTK+ */
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
gtk_init (&argc, &argv);
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
/* Create the main window */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW (win), 400, 300);
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
gtk_window_set_title (GTK_WINDOW (win), "test");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_realize (win);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);
/* Create a vertical box with buttons */
vbox = gtk_vbox_new (TRUE, 6);
gtk_container_add (GTK_CONTAINER (win), vbox);
button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
g_signal_connect (button, "clicked", gtk_main_quit, NULL);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
/* Enter the main loop */
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
The code was taken from a "Hello world" example I found on the internet a while back.
I'm aware that this issue have already gotten an answer, but my situation is much more complex (from my perspective at least). First off, I've installed all packages required. I run Ubuntu 14.04 by the way.
When I compile the code using g++ main.cpp I get the following error:
main.cpp:2:21: fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
^
compilation terminated.
There is a fix for this particular error, which is to extend the compile command like this: g++ main.cpp -I/usr/include/gtk-2.0. This however will provide another, similar error:
In file included from /usr/include/gtk-2.0/gdk/gdk.h:32:0,
from /usr/include/gtk-2.0/gtk/gtk.h:32,
from main.cpp:2:
/usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30:21: fatal error: gio/gio.h: No such file or directory
#include <gio/gio.h>
^
compilation terminated.
You can fix this as well by extending the command like this (all commands are fund on the internet and I don't quite understand it all): g++ -g -Wall -c -o program.o main.cpp -I/usr/include/gtk-2.0 $(pkg-config --libs --cflags glib-2.0). There will now be an error with cairo.h.
As you can see there are similar errors. I have no idea what's wrong but I must believe there is a relatively easy fix.
Also, I tried a fresh install of Ubuntu (just installed packages necessary) and the same errors occur.
The pkgconfig command gives you all of the necessary -I (include) and -l (linker) statements for the compiler, when including a certain package.
Take a look what is given by running:
pkgconfig --cflags --libs gtk+-2.0
I've tried compiling your code on my Ubuntu 14.04.1 and it went fine, when I used:
g++ main.cpp `pkg-config --cflags --libs gtk+-2.0`
Probably pkg-config --libs --cflags glib-2.0 wasn't enough to provide all of the necessary include paths and shared libraries.
It's works (Ubuntu):
export CPATH=$CPATH:/usr/include/gtk-2.0
export CPATH=$CPATH:/usr/include/glib-2.0/
export CPATH=$CPATH:/usr/lib/glib-2.0/include/
export CPATH=$CPATH:/usr/include/pango-1.0/
export CPATH=$CPATH:/usr/lib/gtk-2.0/include/
export CPATH=$CPATH:/usr/include/atk-1.0/
With Fedora >=25:
export CPATH=$CPATH:/usr/include/gtk-3.0
export CPATH=$CPATH:/usr/include/glib-2.0/
export CPATH=$CPATH:/lib64/glib-2.0/include/
export CPATH=$CPATH:/usr/include/pango-1.0/
export CPATH=$CPATH:/usr/lib64/gtk-3.0/3.0.0/
export CPATH=$CPATH:/usr/include/atk-1.0/
export CPATH=$CPATH:/usr/include/cairo/
into ~/.bashrc
or
into ~/.zshrc
Remember to run: source ~/.bashrc
When you don't have a file installed on Ubuntu, the place to go is http://packages.ubuntu.com and use the second search form to look for the filename.
In this case, the answer is (for trusty):
File Package
-----------------------------------------------------------------
/usr/include/gtk-2.0/gtk/gtk.h libgtk2.0-dev
/usr/include/gtk-3.0/gtk/gtk.h libgtk-3-dev
/usr/lib/R/site-library/RGtk2/include/RGtk2/gtk.h r-cran-rgtk2
You need to select which one of those is most suitable, and install it.
I'm guessing this one: libgtk2.0-dev
You can then repeat the process for the gio/gio.h header file.
I also immigranted to C++ from Python and I am just not used with how the C++ include way of working. Here's what I do to make life easier in C++. I make a Makefile with the following contents:
all:
g++-5 main.cpp -o main `pkg-config --libs --cflags gtkmm-3.0`
Instead of main.cpp, include all your *.cpp files and headers. (Also, please notice I use gtkmm-3.0) And in gtkmm-3.0, your Application with one window, one button and the button being linked to a function [which says: Hello World! of course] would look like this
#include <iostream>
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
class HelloWorld : public Gtk::Window{
public:
HelloWorld();
virtual ~HelloWorld();
protected:
//Signal handlers:
void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};
// implementing the class HelloWorld
HelloWorld::HelloWorld(): m_button("Click me, Xweque"){
set_border_width(20); // Sets the border width of the window.
// Connecting the button's clicked signal to an arbitrary function
m_button.signal_clicked().connect(sigc::mem_fun(*this, &HelloWorld::on_button_clicked));
add(m_button);
m_button.show();
}
HelloWorld::~HelloWorld()
{
}
void HelloWorld::on_button_clicked(){
std::cout << "Hello World" << std::endl;
}
int main(int argc, char** argv){
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
HelloWorld mywindow;
app->run(mywindow);
return 0;
}
I'm building a simple C++ application on Mac OS X 10.9 with Qt 5.2.1 using CMake (without MOC).
I am starting the executable from the command-line. The problem is that the menu bar is not showing up at all, the Terminal menu bar is still visible but not clickable. When I switch windows temporarily and then come back to the window of this application, I at least see the standard "application" menu with "About". The "About" action is now working and shows the dialog. The toolbar button also works as expected.
What else I tried (and didn't work):
using the pre-defined menuBar()
use setMenuBar()
new menuBar(0)
menubar->setVisible(true)
When I check the isVisible() it returns false, also if I set it to visible in the line before.
I wonder whether the lack of using MOC can be the reason for this?
Below I attached a reduced example.
#include <QtGui>
#include <QtWidgets>
class MainWindow : public QMainWindow {
public:
MainWindow();
private:
void create_actions_();
void create_menus_();
void create_toolbar_();
void about_();
QMenuBar* menu_bar_;
QMenu* file_menu_;
QMenu* help_menu_;
QToolBar* file_toolbar_;
QAction* action_about_;
};
MainWindow::MainWindow() {
resize(800, 600);
create_actions_();
create_menus_();
create_toolbar_();
}
void MainWindow::create_actions_() {
action_about_ = new QAction(tr("About"), this);
connect(action_about_, &QAction::triggered, this, &MainWindow::about_);
}
void MainWindow::create_menus_() {
menu_bar_ = new QMenuBar(this);
file_menu_ = menu_bar_->addMenu(tr("&File"));
menu_bar_->addSeparator();
help_menu_ = menu_bar_->addMenu(tr("&Help"));
help_menu_->addAction(action_about_);
menu_bar_->setNativeMenuBar(true);
}
void MainWindow::create_toolbar_() {
file_toolbar_ = addToolBar(tr("File"));
file_toolbar_->addAction(action_about_);
file_toolbar_->setIconSize(QSize(16, 16));
}
void MainWindow::about_() {
QMessageBox::about(this, tr("About"), tr("FooBar"));
}
int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow main_window;
main_window.show();
const int exit_code = app.exec();
return exit_code;
}
CMakeLists.txt
FIND_PACKAGE(Qt5Core)
FIND_PACKAGE(Qt5Gui)
FIND_PACKAGE(Qt5OpenGL)
FIND_PACKAGE(Qt5Widgets)
FIND_PACKAGE(Qt5Declarative)
FIND_PACKAGE(Qt5MacExtras)
ADD_EXECUTABLE(main main.cc)
qt5_use_modules(main Core Gui Widgets Declarative MacExtras)
Thanks a lot in advance!
OK, solved the problem myself. It appears you cannot add a separator to the menubar.
Removing the menu_bar_->addSeparator(); solved the problem.
I have a C++/Qt application which loads a plugin (.dll/.so) using QPluginLoader facilities.
This plugin is basically an embedded python interpreter which allows to inspect Qt objects in the main application via the PyQt4 module.
Problem is the command PyQt4.QtCore.QCoreApplication.instance(), executed from the plugged-in python interpreter, returns None even though the QCoreApplication instance has been created by the C++ application.
This is only on windows in debug mode.
On linux or on release mode on windows, the command PyQt4.QtCore.QCoreApplication.instance() correctly returns the QCoreApplication instance that was created by the C++ application.
Following is some minimalist code showing the problem.
When compiled in release mode:
$ ./a.out
1+1
2
import PyQt4
import PyQt4.QtCore
PyQt4.QtCore.QCoreApplication.instance()
<PyQt4.QtCore.QCoreApplication object at 0x00C69198>
=> Ok
When compiled in debug mode:
$ ./a.out
import PyQt4
import PyQt4.QtCore
PyQt4.QtCore.QCoreApplication.instance()
=> Not ok (returned None)
File main.cpp
#include <QCoreApplication>
#include <QPluginLoader>
#include <QDebug>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QPluginLoader loader("plugin.dll");
loader.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
loader.load();
if(!loader.isLoaded()) {
qDebug() << loader.errorString();
return 1;
}
(void)loader.instance();
return app.exec();
}
File plugin.h
#ifndef PLUGIN_H
#define PLUGIN_H
#include <QObject>
#include <Python.h>
class Plugin : public QObject
{
public:
Plugin();
~Plugin();
private:
PyThreadState *m_ts;
};
class InterpInput : public QObject
{
Q_OBJECT
public:
InterpInput(QObject *parent = 0) : QObject(parent) { }
public slots:
void monitorInput();
signals:
void done();
void inputReady();
};
class InterpOutput : public QObject
{
Q_OBJECT
public:
InterpOutput(QObject *parent = 0) : QObject(parent) { }
public slots:
void processLine();
public:
PyThreadState *m_ts;
};
#endif
File plugin.cpp
#include "plugin.h"
#include <QCoreApplication>
#include <QThread>
#include <QtPlugin>
#include <QPluginLoader>
Q_EXPORT_PLUGIN2(Plugin, Plugin)
Plugin::Plugin()
{
Py_Initialize();
PyEval_InitThreads();
InterpInput *in = new InterpInput();
InterpOutput *out = new InterpOutput(this);
in->connect(in, SIGNAL(inputReady()), out, SLOT(processLine()));
in->connect(in, SIGNAL(done()), QCoreApplication::instance(), SLOT(quit()));
QThread *thr = new QThread(this);
in->moveToThread(thr);
thr->connect(thr, SIGNAL(started()), in, SLOT(monitorInput()));
m_ts = PyEval_SaveThread();
out->m_ts = m_ts;
thr->start();
}
Plugin::~Plugin()
{
PyEval_RestoreThread(m_ts);
Py_Finalize();
}
void InterpInput::monitorInput()
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
int ret = PyRun_SimpleString("import sys\nimport code\nic = code.InteractiveConsole()");
assert(ret == 0);
while(true) {
ret = PyRun_SimpleString("line = ic.raw_input()");
if(ret) { break; }
inputReady();
}
done();
PyGILState_Release(gstate);
}
void InterpOutput::processLine()
{
PyEval_RestoreThread(m_ts);
int ret = PyRun_SimpleString("ic.push(line)");
PyRun_SimpleString("sys.stdout.flush()");
PyRun_SimpleString("sys.stderr.flush()");
(void)PyEval_SaveThread();
assert(ret == 0);
}
File Makefile
MOC=/cygdrive/c/Qt/4.8.4/bin/moc
GCC=/cygdrive/c/MinGW/bin/mingw32-g++.exe
FLAGS=-Ic:/Qt/4.8.4/include -Ic:/Qt/4.8.4/include/QtCore -Lc:/Qt/4.8.4/lib -Lc:/Qt/4.8.4/bin -lQtCore4 -Lc:/Python27/libs -lpython27 -Ic:/Python27/include -DQT_NO_DEBUG
#FLAGS=-Ic:/Qt/4.8.4/include -Ic:/Qt/4.8.4/include/QtCore -Lc:/Qt/4.8.4/bin -lQtCored4 -Lc:/Python27/libs -lpython27 -Ic:/Python27/include -g
LIBFLAGS=-shared
all:
$(MOC) plugin.h > plugin_moc.cpp
$(GCC) -o a.out main.cpp $(FLAGS)
$(GCC) -o plugin.dll $(LIBFLAGS) plugin.cpp plugin_moc.cpp $(FLAGS)
The explanation is the following.
In debug version, the C++ application and plugin link to debug Qt libraries (QtCored4.dll, etc.)
Whereas the installed PyQt4 modules (QtCore.pyd, etc.) link to release Qt libraries (QtCore4.dll, etc.)
It follows that the C++ application, and the PyQt4 module, each see a QCoreApplication instance but each see a different one, which lives in a different library (respectively the debug, and the release, version of the Qt library).
It follows that the instance living in the PyQt4 module is not initialized when the C++ application is initialized, and is thus null.
This is verified by compiling the PyQt4 modules specifying that they should be linked to the debug Qt libraries: the problem then disappears as the debug-mode C++ application and the PyQt4 modules link to the same Qt library.