How signal and slots are implemented under the hood?

后端 未结 2 1855
眼角桃花
眼角桃花 2020-12-05 14:31

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 funct

2条回答
  •  天命终不由人
    2020-12-05 14:59

    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.

提交回复
热议问题