This question is already asked in this forum but I don't understand the concept.
I was reading around and it seems that signal and slots are implemented using function pointers i.e the signal is one big function which inside it calls all connected slots (function pointers). Is this correct? And what is the role of the generated moc files in the whole story? I don't understand how the signal function knows which slots to call i.e which slots are connected to this signal.
Thanks for your time
In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.
In computers, a slot, or expansion slot , is an engineered technique for adding capability to a computer in the form of connection pinholes (typically, in the range of 16 to 64 closely-spaced holes) and a place to fit an expansion card containing the circuitry that provides some specialized capability, such as video ...
if several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.
Qt implements these things in a way that resembles interpreted languages. I.e. it constructs symbol tables that map signal names to function pointers, maintains them and looks up the function pointer by function name where needed.
Each time you emit a signal, i.e. write
emit something();
you actually call the something()
function, which it automatically generated by meta object compiler and placed into a *.moc
file. Within this function it's checked what slots this signal is connected to at the moment, and appropriate slot functions (which you implemented in your own sources) are sequentially called via the symbol tables (in the way described above). And emit
, like other Qt-specific keywords, are just discarded by C++ preprocessor after *.moc
were generated. Indeed, in one of the Qt headers (qobjectdefs.h), there exist such lines:
#define slots #define signals protected #define emit
Connection function (connect
) just modifies the symbol tables maintained within *.moc
files, and the arguments passed to it (with SIGNAL()
and `SLOT macros) are also preprocessed to match the tables.
That's the general idea. In his or her another answer, ジョージ supplies us with links to trolltech mailing list and to another SO question on this topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With