Search code examples
qtsignals-slotsdynamic-binding

how is the connection between signal and slot made in QT?


I have been a Qt programmer for quite some time now and i understand most of the general features of Qt. I am still confused about how the connect statement connects a signals to a slot at run time. Basically i would like to understand what happens at compile time and what happens at run time..

compile time: meta object compiler will generate code to implement a signal in an additional cpp file (one for each class containing Q_OBJECT).

run time: signal is mapped to a slot, slot gets executed? this is the part i am not clear about...SIGNAL and SLOTS are macros that expand to string representation of the signal/slot names...how does this and the meta object help in mapping calls to slots at run time? details would be appreciated...

EDIT: this link will give you a better idea..(only if you are interested in the gory details...) http://dev.libqxt.org/libqxt/wiki/Meta_Object_Format

couple this with the documentation of QMetaObject and things should become clear...


Solution

  • There are various ways you can connect a signal to a method (signal/slot).

    However, they all revolve around getting the correct method number.

    After you have the correct method number and the object to call it on, you simply call a virtualized function (qt_metacall) in QObject which finds the correct method to call from the number given. You can find this function in the files that the MOC generates. Also, in that generated file, you can find a line which creates a static QMetaObject of your class. This object registers the names of your slots and signals to the method numbers.

    These might provide some interesting stuff to read:

    http://doc.qt.io/qt-5/qmetaobject.html
    http://doc.qt.io/qt-5/metaobjects.html
    http://doc.qt.io/qt-5/signalsandslots.html

    You can also learn a lot by running thought the slot activations with a debugger.