Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Q_OBJECT vs. #include <QObject>

Tags:

c++

qt

qobject

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.

like image 426
Simplicity Avatar asked Dec 10 '22 09:12

Simplicity


2 Answers

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.

like image 176
Mat Avatar answered Dec 30 '22 11:12

Mat


They are used for two different purposes.

Q_OBJECT:

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.

If you want to use signals,slots and other meta-object features, you need to have the Q_OBJECT macro.

#include<QObject>:

This is used to include qobject.h header file, so that you can use the functions available in the class QObject.. Say for eg., QObject::connect().

And to use the Q_OBJECT macro you have to #include <QObject>.

like image 42
liaK Avatar answered Dec 30 '22 10:12

liaK