c++ - How to use GTK signals? -
i new gtk , i'm coming qt background. i'm trying figure out how signals work in gtk , i'm trying emit 1 doesn't work. found surprising couldn't find decent code example emits gtk signal works. code far(i'm using qt creator):
test_gtk.pro:
#------------------------------------------------- # # project created qtcreator 2015-04-26t12:42:38 # #------------------------------------------------- qt += core gui greaterthan(qt_major_version, 4): qt += widgets target = test_gtk template = app sources += main.cpp \ myfirstobject.cpp \ mysecondobject.cpp headers += \ myfirstobject.h \ mysecondobject.h unix:!macx{ # make sure install libappindicator-dev includepath += /usr/include/glib-2.0 includepath += /usr/include/gtk-2.0 includepath += /usr/lib/x86_64-linux-gnu/glib-2.0/include includepath += /usr/lib/x86_64-linux-gnu/gtk-2.0/include includepath += /usr/include/cairo includepath += /usr/include/pango-1.0 includepath += /usr/include/gdk-pixbuf-2.0 includepath += /usr/include/atk-1.0 libs += -l/usr/lib/x86_64-linux-gnu -lgobject-2.0 libs += -l/usr/lib/x86_64-linux-gnu -lgtk-x11-2.0 } myfirstobject.h:
#ifndef myfirstobject_h #define myfirstobject_h #include <qobject> #ifdef q_os_linux #undef signals extern "c" { #include <gtk/gtk.h> } #define signals public #endif class myfirstobject : public gobject { public: myfirstobject(); ~myfirstobject(); void emitmysignal(); }; #endif // myfirstobject_h myfirstobject.cpp:
#include "myfirstobject.h" myfirstobject::myfirstobject() { g_signal_new("my-signal", g_type_object, g_signal_run_first, 0, null, null, g_cclosure_marshal_void__pointer, g_type_none, 1, g_type_pointer); } myfirstobject::~myfirstobject() { } void myfirstobject::emitmysignal() { g_signal_emit_by_name (this, "my-signal"); } mysecondobject.h:
#ifndef mysecondobject_h #define mysecondobject_h #include <qobject> #ifdef q_os_linux #undef signals extern "c" { #include <gtk/gtk.h> } #define signals public #endif class myfirstobject; class mysecondobject : public gobject { public: mysecondobject(myfirstobject *obj); ~mysecondobject(); void myslot(); private: myfirstobject *m_obj; }; #endif // mysecondobject_h mysecondobject.cpp:
#include "mysecondobject.h" #include "myfirstobject.h" #include <qdebug> mysecondobject::mysecondobject(myfirstobject *obj) : m_obj(obj) { g_signal_connect(m_obj, "my-signal", g_callback(&mysecondobject::myslot), 0); } mysecondobject::~mysecondobject() { } void mysecondobject::myslot() { qdebug() << "myslot called"; } main.cpp:
#include <qcoreapplication> #include "myfirstobject.h" #include "mysecondobject.h" int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); myfirstobject *o1 = new myfirstobject; mysecondobject *o2 = new mysecondobject(o1); o1->emitmysignal(); return a.exec(); } the code simple. emit signal myfirstobject , expect callback called in mysecondobject. problem callback never called. can please tell me doing wrong? there place gtk signals explained or code examples?
as far learned far gobject in gtk qobject in qt , gtkobject in gtk qwidget in qt. true?
the gtkmm tutorial recommends using libsigc++ create signals c++ code; gtkmm uses libsigc++ internally wrap signals defined in c in gtk+ itself.
https://developer.gnome.org/gtkmm-tutorial/stable/chapter-custom-signals.html.en
a (too verbose copy here) example provided too: https://developer.gnome.org/gtkmm-tutorial/stable/chapter-custom-signals-example.html.en
Comments
Post a Comment