3

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...

maxpayne
  • 2,479
  • 4
  • 28
  • 28

2 Answers2

2

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.

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
0xbaadf00d
  • 2,535
  • 2
  • 24
  • 46
  • thanks for the answer..i can see the qt_metacall function in the generated in the moc file....but the source object's moc file has information about the signal only not the slot...so how does it find the slot? – maxpayne Mar 14 '11 at 12:19
  • I'm not certain if I understand. The slots and signals are registered through the static meta object generated in the MOC file. Does the MOC file not list your slots? It should, mine does. It might be confusing since the signals and slots are all in one heap. Could you please give a more thorough explanation so I can understand. – 0xbaadf00d Mar 14 '11 at 13:34
0

Basically signals and slots work similar to messages in Objective-C.

The macros cause the preprocessor to replace them with some code which "registers" and "looks up" the functions/methods that are to be effectively executed when a slot is called.

This allows for more flexibility, because the code that is emitting a signal or calling a slot does not need to know much about the other code modules which use them. Each slot and signal generate a signature that is looked up at runtime and then called.

If you are familiar with C/C++, you can compare this to dynamic libraries. Symbols are looked up at runtime and their address is then used to let the CPU "jump" to them to execute.

Also, these links might help you:

Community
  • 1
  • 1
BastiBen
  • 19,679
  • 11
  • 56
  • 86
  • thanks for the answer..i was looking for explicit details..i am quite familiar with the QT documentation and other posts.i am particularly interested in how and where that generated signature is looked up a run time and what role does the meta object play in this.. – maxpayne Mar 14 '11 at 11:46
  • How about this? http://doc.qt.nokia.com/latest/signalsandslots.html#meta-object-information and this http://doc.qt.nokia.com/latest/metaobjects.html#meta-object-system – BastiBen Mar 14 '11 at 11:58