简体   繁体   English

QObject错误与宏和包括

[英]QObject error with macro and include

I want to use signal and slot in my program and for this I am told Ineed to add Q_OBJECT as below. 我想在我的程序中使用信号和插槽,为此我被告知Ineed添加如下的Q_OBJECT。

Well I have a class: 好吧,我有一节课:

class A
{
    Q_OBJECT
public:
    A();
};

This gives an error which says 'Q_OBJECT does not name a type'. 这会产生一个错误,上面写着'Q_OBJECT没有命名类型'。 If I than add #include It give the error 'undefined reference to vtable of A' 如果我要添加#include它给错误'未定义的引用到Vtable的A'

So what is the right way to do this? 那么这样做的正确方法是什么?

The Q_OBJECT macro is meant for subclasses of a QObject (or other subclasses). Q_OBJECT宏适用于QObject (或其他子类)的子类。 It is also required if you want your class to use signals and slots. 如果您希望班级使用信号和插槽,也需要它。

class A 
    : public QObject
{
    Q_OBJECT

 public:
    A(QObject *parent = 0);
};

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的元对象系统提供的其他服务。
... ...
Note: 注意:
This macro requires the class to be a subclass of QObject. 该宏要求该类是QObject的子类。 ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM