views:

367

answers:

1

Hello

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

+3  A: 

Disclaimer: this is only a concept of how things may happen. I didn't dig into Qt for a long time, so the current state of art may differ from my description, although it should give the general idea.

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). By the way, I am pretty sure that somewhere in Qt headers there exists a line:.

#define emit

Connection function just modifies the symbol tables maintained within *.moc files.

That's the general idea.

Pavel Shved
Basically correct, you can set a breakpoint at the the emit and then step through the signalling process. While usually delivered directly, signals mayb be queued, this is necessary when you want to connect two objects in different threads
Harald Scheirich