Results 1 to 2 of 2

Thread: Including Q_OBJECT in another macro

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Including Q_OBJECT in another macro

    Hi everyone,

    I encountered an issue where visual studio complains the class declarations lacks Q_OBJECT macro.

    The class definition looked something like this before:

    Qt Code:
    1. class Abc : QObject
    2. {
    3. Q_OBJECT
    4. MY_CUSTOM_MACRO(Abc)
    5. public:
    6. Abc();
    7. };
    To copy to clipboard, switch view to plain text mode 

    Because we use the MY_CUSTOM_MACRO along with Q_OBJECT a lot, I have attempt to refactor the code so Q_OBJECT is now part of the MY_CUSTOM_MACRO. So I have changed definition of MY_CUSTOM_MACRO from this:

    Qt Code:
    1. #define MY_CUSTOM_MACRO (E) \
    2. public: \
    3. void* operator new( size_t size ) \
    4. { \
    5. void *storage = malloc(size); \
    6. return storage; \
    7. } \
    8. private:
    To copy to clipboard, switch view to plain text mode 

    to this:

    Qt Code:
    1. #define MY_CUSTOM_MACRO (E) \
    2. Q_OBJECT \
    3. public: \
    4. void* operator new( size_t size ) \
    5. { \
    6. void *storage = malloc(size); \
    7. return storage; \
    8. } \
    9. private:
    To copy to clipboard, switch view to plain text mode 

    And the declaration of class Abc to this:

    Qt Code:
    1. class Abc : QObject
    2. {
    3. MY_CUSTOM_MACRO(Abc)
    4. public:
    5. Abc();
    6. };
    To copy to clipboard, switch view to plain text mode 

    This is when the error of missing Q_OBJECT macro starts popping up. Does moc or something scans the header file for Q_OBJECT without expanding MY_CUSTOM_MACRO or something and hence causing the error? Or is it something else I missed?

    Thuan

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Including Q_OBJECT in another macro

    Does moc or something scans the header file for Q_OBJECT without expanding MY_CUSTOM_MACRO or something and hence causing the error?
    Got it in one. moc does not include a complete C++ preprocessor or have access to the entire (compiler dependent) source code environment to allow this.

Similar Threads

  1. Q_OBJECT macro problem
    By bijan311 in forum Newbie
    Replies: 46
    Last Post: 26th March 2010, 22:21
  2. Problems with the Q_OBJECT-Macro
    By tokstolle in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2009, 17:09
  3. Problems with Q_OBJECT macro
    By dbrmik in forum Qt Programming
    Replies: 21
    Last Post: 26th February 2009, 14:10
  4. Q_OBJECT macro - what exactly does it do?
    By magland in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2007, 10:30
  5. Q_OBJECT macro issue
    By kandalf in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2007, 19:28

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
  •  
Qt is a trademark of The Qt Company.