When should Q_OBJECT be used?
The documentation states that:
The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other servic开发者_开发知识库es provided by Qt's meta-object system.
But exactly what does that mean? On which QObject-derived classes can I safely omit it? Will problems arise if you omit Q_OBJECT on a QObject-derived class, and then inherit from that one? Basically I would like a little more information on when I can omit it from my Qt classes.
You should use the Q_OBJECT
macro for any non-templated classes that derive from QObject
.
Besides signals and slots, the Q_OBJECT
macro provides the meta object information that is associated with given class.
As stated in the documentation:
we strongly recommend that all subclasses of QObject use the Q_OBJECT macro regardless of whether or not they actually use signals, slots, and properties.
Suppose we have the following class:
class Class : public QObject {
public:
Class() {}
};
Without Q_OBJECT
, the following metaobject system features (among others) will not work for Class
:
qobject_cast<Class>()
- due to missing metadataQObject::tr()
- due to missing metadataslots and invokables first declared in
Class
, when invoked or looked up by name - none ofQMetaObject
methods will work for these methods, neither will the Qt 4connect
- due to missing metadatasignals - since
moc
won't generate their implementations and the code won't compile.
You can omit it, of course, but if you ever use these features, you'll need to remember to put the macro into the class's declaration. This is a rather brittle practice and best avoided. The savings are not worth it. So, don't wait - add the Q_OBJECT
macro to every class that derives from QObject
as a matter of coding policy.
The Q_OBJECT
macro should never be used on classes that don't derive from QObject
. To add invokables and properties to such classes, use the Q_GADGET
macro instead.
If you want to use signals/slots you MUST include the Q_OBJECT macro and derive the class from QObject.
Otherwise you can leave it out, but it doesn't do any harm to include it in all the Qt gui classes
Well the first part is pretty clear as you probably already know.. signals and slots, the rest of the Meta-object system is a little lesser known. Perhaps one of the more useful features is dynamic properties. Although these have many uses, I used them to take advantage of Qt's animation system QPropertyAnimation
.
There's a little more info about the meta-object system here: http://doc.qt.io/archives/4.6/metaobjects.html
I think the bottom line is, if you inherit from the QObject hierarchy, throw in the Q_OBJECT macro regardless. It's simple to do and will save you from some potentially baffling problems down the road.
What @liaK said is correct (in short: you should always use the Q_OBJECT macro in any class that derives from QObject).
One thing that I haven't seen highlighted is that if you don't explicitly put the Q_OBJECT macro then using the sometimes very handy qobject_cast won't work!!!
精彩评论