title: How to use GTK signals?
body: <p>I am new to <code>GTK</code> and I'm coming from a <code>Qt</code> background. I'm trying to figure out how signals work in <code>GTK</code> and I'm trying to emit one but it doesn't work. I found it surprising that I couldn't find a decent code example which emits a <code>GTK</code> signal that works. This is my code so far(I'm using <code>Qt Creator</code>):</p>
  
  <p><em>Test_Gtk.pro</em>:</p>
  
  <pre><code>#-------------------------------------------------
  #
  # Project created by 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 you 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
  }
  </code></pre>
  
  <p><em>myfirstobject.h</em>:</p>
  
  <pre><code>#ifndef MYFIRSTOBJECT_H
  #define MYFIRSTOBJECT_H
  
  #include &lt;QObject&gt;
  
  #ifdef Q_OS_LINUX
  #undef signals
  extern "C" {
      #include &lt;gtk/gtk.h&gt;
  }
  #define signals public
  #endif
  
  class MyFirstObject : public GObject
  {
  public:
      MyFirstObject();
      ~MyFirstObject();
  
      void emitMySignal();
  };
  
  #endif // MYFIRSTOBJECT_H
  </code></pre>
  
  <p><em>myfirstobject.cpp</em>:</p>
  
  <pre><code>#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");
  }
  </code></pre>
  
  <p><em>mysecondobject.h</em>:</p>
  
  <pre><code>#ifndef MYSECONDOBJECT_H
  #define MYSECONDOBJECT_H
  
  #include &lt;QObject&gt;
  
  #ifdef Q_OS_LINUX
  #undef signals
  extern "C" {
      #include &lt;gtk/gtk.h&gt;
  }
  #define signals public
  #endif
  
  class MyFirstObject;
  
  class MySecondObject : public GObject
  {
  public:
      MySecondObject(MyFirstObject *obj);
      ~MySecondObject();
  
      void mySlot();
  
  private:
      MyFirstObject *m_obj;
  };
  
  #endif // MYSECONDOBJECT_H
  </code></pre>
  
  <p><em>mysecondobject.cpp</em>:</p>
  
  <pre><code>#include "mysecondobject.h"
  #include "myfirstobject.h"
  
  #include &lt;QDebug&gt;
  
  MySecondObject::MySecondObject(MyFirstObject *obj) : m_obj(obj)
  {
      g_signal_connect(m_obj, "my-signal", G_CALLBACK(&amp;MySecondObject::mySlot), 0);
  }
  
  MySecondObject::~MySecondObject()
  {
  
  }
  
  void MySecondObject::mySlot()
  {
      qDebug() &lt;&lt; "mySlot was called";
  }
  </code></pre>
  
  <p><em>main.cpp</em>:</p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  
  #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-&gt;emitMySignal();
  
      return a.exec();
  }
  </code></pre>
  
  <p>The code is simple. I emit a signal from <code>MyFirstObject</code> and I expect a callback to be called in <code>MySecondObject</code>. The problem is that the callback is never called. Can you please tell me what am I doing wrong? Is there a place where <code>GTK</code> signals are well explained or some code examples?</p>
  
  <p>As far as I learned so far <code>GObject</code> in <code>GTK</code> is like <code>QObject</code> in <code>Qt</code> and <code>GtkObject</code> in <code>GTK</code> is like <code>QWidget</code> in <code>Qt</code>. Is this true?</p>