Results 1 to 5 of 5

Thread: Should I have to declare Q_OBJECT when I subclass a QWidget?

  1. #1
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Should I have to declare Q_OBJECT when I subclass a QWidget?

    I've learned that Q_OBJECT will have moc generate moc file and what moc file is.
    If I subclass a custom widget, and there will be no new signal definition, is it a MUST that I have to declare Q_OBJECT?
    I tried a test without Q_OBJECT, looked like it worked. But I'm eager to know what I may miss.

    thx

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Should I have to declare Q_OBJECT when I subclass a QWidget?

    In theory, no, you don't need the Q_OBJECT macro.
    In practice, it is advised to always use the Q_OBJECT macro when dealing with the metaobject compiler.

  3. #3
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Should I have to declare Q_OBJECT when I subclass a QWidget?

    Quote Originally Posted by tbscope View Post
    In practice, it is advised to always use the Q_OBJECT macro when dealing with the metaobject compiler.
    oh yeah... some weeks ago, I spent a lot of time resolving an error. I added a signal to an older custom QWidget subclass but forgot to add Q_OBJECT...

  4. #4
    Join Date
    Jan 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Should I have to declare Q_OBJECT when I subclass a QWidget?

    Quote Originally Posted by tbscope View Post
    In theory, no, you don't need the Q_OBJECT macro.
    In practice, it is advised to always use the Q_OBJECT macro when dealing with the metaobject compiler.
    Thanks, it's helpful

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Should I have to declare Q_OBJECT when I subclass a QWidget?

    There is a nice side effect of not adding Q_OBJECT macro to a class -- you can hide the class in the QObject inheritance line. Of course it can be an advantage or a disadvantage (e.g. you can't qobject_cast to the class if it doesn't have the macro).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Can't declare more than 7 QStrings ?!?!?!
    By ruben.rodrigues in forum Newbie
    Replies: 8
    Last Post: 21st July 2010, 14:42
  2. Replies: 8
    Last Post: 12th February 2010, 02:41
  3. QWidget - Q_OBJECT makes the widget disappear
    By Sir Rogers in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2010, 23:39
  4. Replies: 2
    Last Post: 1st January 2008, 13:31
  5. declare in designer
    By mickey in forum Newbie
    Replies: 1
    Last Post: 23rd February 2006, 19:24

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.