Qt - Q_OBJECT vs. #include 。

时间:2022-11-21 18:45:54

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

使用Q_Object宏和#include 是否具有相同的效果?换句话说,它们是同一目的的两种不同的方式吗?

Thanks.

谢谢。

3 个解决方案

#1


10  

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

不。除了包含正确的标题之外,您还需要在需要信号/插槽的事物的类定义中使用Q_OBJECT。

From the QObject api docs :

来自QObject api文档:

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.

注意,任何实现信号、槽或属性的对象都必须使用Q_OBJECT宏。您还需要在源文件上运行元对象编译器。我们强烈建议在QObject的所有子类中使用这个宏,不管它们是否实际使用信号、槽和属性,因为没有这样做可能会导致某些函数出现奇怪的行为。

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.

查看Qt文档中的各种示例,了解更多细节和示例代码。信号和槽的引用是一个很好的地方。还要查看对象模型引用。

#2


3  

They are used for two different purposes.

它们用于两个不同的目的。

Q_OBJECT:

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.

Q_OBJECT宏必须出现在定义自己的信号和槽的类定义的私有部分中,或者使用Qt的元对象系统提供的其他服务。

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

如果您想要使用信号、插槽和其他元对象特性,您需要拥有Q_OBJECT宏。

#include<QObject>:

# 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().

这是用来包含qobject的。h头文件,以便您可以使用类QObject中可用的函数。如说。QObject::connect()。

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

要使用Q_OBJECT宏,必须包括

#3


0  

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

不。将这个宏添加到来自QObject的类。但是你仍然需要标题如Mat说!看到http://doc.qt.io/qt-5/qobject.html Q_OBJECT

#1


10  

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

不。除了包含正确的标题之外,您还需要在需要信号/插槽的事物的类定义中使用Q_OBJECT。

From the QObject api docs :

来自QObject api文档:

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.

注意,任何实现信号、槽或属性的对象都必须使用Q_OBJECT宏。您还需要在源文件上运行元对象编译器。我们强烈建议在QObject的所有子类中使用这个宏,不管它们是否实际使用信号、槽和属性,因为没有这样做可能会导致某些函数出现奇怪的行为。

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.

查看Qt文档中的各种示例,了解更多细节和示例代码。信号和槽的引用是一个很好的地方。还要查看对象模型引用。

#2


3  

They are used for two different purposes.

它们用于两个不同的目的。

Q_OBJECT:

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.

Q_OBJECT宏必须出现在定义自己的信号和槽的类定义的私有部分中,或者使用Qt的元对象系统提供的其他服务。

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

如果您想要使用信号、插槽和其他元对象特性,您需要拥有Q_OBJECT宏。

#include<QObject>:

# 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().

这是用来包含qobject的。h头文件,以便您可以使用类QObject中可用的函数。如说。QObject::connect()。

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

要使用Q_OBJECT宏,必须包括

#3


0  

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

不。将这个宏添加到来自QObject的类。但是你仍然需要标题如Mat说!看到http://doc.qt.io/qt-5/qobject.html Q_OBJECT