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.
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.
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With