开发者

Qt - Q_OBJECT vs. #include <QObject>

Does using Q_Object macro and #include <QObject> have the same effect? In other words, are they two different ways f开发者_高级运维or 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>.


No. You add this macro to classes derived from QObject. But you still need the headers as Mat says! See http://doc.qt.io/qt-5/qobject.html#Q_OBJECT

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜