Results 1 to 4 of 4

Thread: Q_OBJECT macro - what exactly does it do?

  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Q_OBJECT macro - what exactly does it do?

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Q_OBJECT macro - what exactly does it do?

    Quote Originally Posted by magland View Post
    * 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

  3. The following user says thank you to wysota for this useful post:

    magland (26th September 2007)

  4. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Thanks
    43
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Q_OBJECT macro - what exactly does it do?

    Wysota, you are the best

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Q_OBJECT macro - what exactly does it do?

    Quote Originally Posted by magland View Post
    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/...2_4/video.html

Similar Threads

  1. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 17:04
  2. Macro
    By comlink21 in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 11:28
  3. Macro used for debugging
    By sunil.thaha in forum General Programming
    Replies: 11
    Last Post: 30th March 2007, 17:32
  4. Q_OBJECT macro issue
    By kandalf in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 19:28
  5. qDebug macro substitution
    By the_bis in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2006, 08:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.