Qt - Q_OBJECT vs. #include <QObject>

Simplicity picture Simplicity · Apr 14, 2011 · Viewed 9.1k times · Source

Does using Q_Object macro and #include <QObject> have the same effect? In other words, are they two different ways for the same purpose?

Thanks.

Answer

Mat picture Mat · Apr 14, 2011

No. You need Q_OBJECT in the class definition of things you want signals/slots on, in addition to having the right headers included.

From the QObject api docs :

Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.

Just look at the various examples in the Qt documentation for more details and sample code. The Signals and Slots reference is a good place to look. Also look at the Object Model reference.