这个类没问题:

#include <QThread>

class LiveImageItem : public QThread
{
    Q_OBJECT
public:
    LiveImageItem(QPixmap pimg);

signals:

public slots:

};

但是此类会出现与头文件中定义的“Q_OBJECT”宏相关的问题
#include <QGraphicsPixmapItem>

class LiveImageItem : public QGraphicsPixmapItem
{

    Q_OBJECT //this line will generate many errors in compiling

public:
    LiveImageItem(QPixmap pimg);

signals:

public slots:

};

它们的cpp文件都相同:
#include "LiveImageItem.h"

LiveImageItem::LiveImageItem(QPixmap pimg)
{
}

我以为每个QT对象本质上都是从QObject继承的,所以,如果我继承QObject的任何子类,那么我将拥有QObject提供的所有魔力。上面的第二个版本(继承自QGraphicsPixmapItem)似乎证明我错了。事实证明,编译时会出现很多错误,所有错误均来自moc文件(由QT自动生成)。怎么了?

其中一些错误是:
  • [qobject.h]错误:“QScopedPointer QObject::d_ptr”为
    protected
  • [moc_LiveImageItem.cpp]错误:在此上下文中
  • ...
  • 最佳答案

    根据文档,QGraphicsPixmapItem不是QObject,因此您不能像对待它那样对待它。我会尝试扩展您的类继承并这样做:

    class LiveImageItem : public QObject, public QGraphicsPixmapItem
    {
    
        Q_OBJECT //this line will generate many errors in compiling
    [..]
    

    09-10 03:29