PDA

View Full Version : Should I have to declare Q_OBJECT when I subclass a QWidget?



myfifth
17th January 2011, 03:10
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

tbscope
17th January 2011, 04:28
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.

FelixB
17th January 2011, 08:09
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...

myfifth
17th January 2011, 08:39
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

wysota
17th January 2011, 13:05
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).