Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt inheriting from QGraphicsEllipseItem

Tags:

qt

I was trying to inherit from QGraphicsEllipseItem 'cause i wanted to add some functionality to it. However i was faced with this error, which probably has something to do with the compiler/precompiler or moc?

error: 'staticMetaObject' is not a member of 'QGraphicsEllipseItem'

And here's the class code:

class MyEllipseItem : public QGraphicsEllipseItem
{
    Q_OBJECT

public:
    MyEllipseItem (const QRectF & outline) : QGraphicsEllipseItem(outline)
    {

    }
};
like image 816
JHollanti Avatar asked Apr 15 '10 11:04

JHollanti


3 Answers

QGraphicsEllipseItem is not QObject, so just remove Q_OBJECT from class declaration.

like image 62
Maxim Popravko Avatar answered Oct 13 '22 20:10

Maxim Popravko


i had a similar error when inheriting from QRunnable which by the the way is NOT a QObject.
Cause

  1. Bad order of inheritence
like image 34
Dr Deo Avatar answered Oct 13 '22 19:10

Dr Deo


If however you need to use some slots/signals in your class you could inherit from QObject as well like the QGraphicsObject does

class MyEllipseItem : public QGraphicsEllipseItem, public QObject
{
    Q_OBJECT

public:
    MyEllipseItem (const QRectF & outline) : QGraphicsEllipseItem(outline)
    {

    }
};

It may be a better idea to inherit from QGraphicsObject and reimplement the ellipse drawing.

For more details check the QGraphicsObject documentation.

like image 30
pnezis Avatar answered Oct 13 '22 20:10

pnezis