PDA

View Full Version : Q_OBJECT macro - what exactly does it do?



magland
26th September 2007, 01:38
I'm hoping somebody can give me a summary of what the Q_OBJECT macro really is.

* Is it a C++ macro in the traditional sense. If so, where is it defined, and what exactly does the preprocessor do when it comes accross Q_OBJECT

* Apparently qmake recognizes the Q_OBJECT macro in project source files. So does qmake actually parse the C++ code and identify all the signals and slots in the source code for the class in which Q_OBJECT is included? This would be surprising to me since it would require parsing of C++ syntax.

* What are "signal:" and "slot:"... apparently they are not macros. Does the compiler recognize these things, or does qmake do a trick and modify all the sources so that the compiler doesn't even see these words?

wysota
26th September 2007, 02:00
* Is it a C++ macro in the traditional sense. If so, where is it defined, and what exactly does the preprocessor do when it comes accross Q_OBJECT
Yes, it's a macro that expands into a meta-data declaration for a class that contains it. The implementation for methods declared by Q_OBJECT are provided by MOC (take a look at moc_* files).


* Apparently qmake recognizes the Q_OBJECT macro in project source files. So does qmake actually parse the C++ code and identify all the signals and slots in the source code for the class in which Q_OBJECT is included? This would be surprising to me since it would require parsing of C++ syntax.
Not exactly. MOC does that :) And it doesn't require C++ syntax parser. It looks for well known tokens (like "signals:" or "slots") and identifies methods declared after them.


* What are "signal:" and "slot:"... apparently they are not macros. Does the compiler recognize these things, or does qmake do a trick and modify all the sources so that the compiler doesn't even see these words?
They are empty macros (really aliases for Q_SLOTS and Q_SIGNALS afair) that serve as tags for moc. As they are empty (not quite... I think "ignals" actually expands to "protected" or "private") compiler doesn't even see them as they are substituted by empty strings by the C++ preprocessor.

Edit:
Baaah... forgot the "must-read" link: http://doc.trolltech.com/qq/qq16-dynamicqobject.html

magland
26th September 2007, 02:32
Wysota, you are the best :)

wysota
26th September 2007, 10:30
Wysota, you are the best :)

No, I'm not :)

Here is another link you might find interesting (Flash 9 required):
http://www.trolltechvideo.com/video/day2/room_b/b_2_4/video.html