Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should Q_OBJECT be used?

Tags:

c++

qt

qobject

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 services 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.

like image 941
Jake Petroules Avatar asked Sep 11 '10 03:09

Jake Petroules


People also ask

What is Q_OBJECT?

QObject is the base class for all Qt classes, Q_OBJECT macro is used to enable meta-object features in classes and finally moc is a preprocessor that changes Q_OBJECT macro instances to C++ source code to enable meta object system mechanism in the class in which it is used.

Why does Qt use MOC for signals and slots?

Because we had the moc for signals and slots, we could add other useful things to it that could not be done with templates. Among these are scoped translations via a generated tr() function, and an advanced property system with introspection and extended runtime type information.

What is QObject in Qt?

QObject is the heart of the Qt Object Model. The central feature in this model is a very powerful mechanism for seamless object communication called signals and slots. You can connect a signal to a slot with connect() and destroy the connection with disconnect().


2 Answers

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:

  1. qobject_cast<Class>() - due to missing metadata

  2. QObject::tr() - due to missing metadata

  3. slots and invokables first declared in Class, when invoked or looked up by name - none of QMetaObject methods will work for these methods, neither will the Qt 4 connect - due to missing metadata

  4. signals - 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.

like image 97
liaK Avatar answered Oct 11 '22 05:10

liaK


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

like image 24
Martin Beckett Avatar answered Oct 11 '22 05:10

Martin Beckett