Question

This class no problem:

#include <QThread>

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

signals:

public slots:

};

BUT this class get problem associated with "Q_OBJECT" macro defined in header file

#include <QGraphicsPixmapItem>

class LiveImageItem : public QGraphicsPixmapItem
{

    Q_OBJECT //this line will generate many errors in compiling

public:
    LiveImageItem(QPixmap pimg);

signals:

public slots:

};

both their cpp file is the same:

#include "LiveImageItem.h"

LiveImageItem::LiveImageItem(QPixmap pimg)
{
}

I thought every QT object essentially inherited from QObject so if I inherit any of the subclass of QObject, I could have all the magics QObject offers. The 2nd version of the above (which is inherited from, say, QGraphicsPixmapItem) seems proved I was wrong. It turns out to be having lots of errors while compiling, all from moc files(automatically generated by QT). What happens?

Some of these errors are:

  • [qobject.h] error: 'QScopedPointer QObject::d_ptr' is protected
  • [moc_LiveImageItem.cpp] error: within this context

  • ...

Was it helpful?

Solution

According to the documentation QGraphicsPixmapItem is not a QObject, thus you cannot treat it as if it is. I would try to extend your class inheritance and do:

class LiveImageItem : public QObject, public QGraphicsPixmapItem
{

    Q_OBJECT //this line will generate many errors in compiling
[..]

OTHER TIPS

As @vahancho said, QGraphicsPixmapItem is not a QObject. In fact, that can be said of most of the QGraphics*Item classes.

However, if you want to use signals and slots with QGraphicsSystem classes, you can inherit from QGraphicsObject: -

class LiveImageItem : public QGraphicsObject
{
    Q_OBJECT

    public:


    private:
        QPixmap m_pixmap;
};

You would then override the paint function in this class and draw the pixmap from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top