PDA

View Full Version : Signals and slots for QGraphicsEllipseItem



Cruz
26th June 2014, 15:08
Hello!

I would like to add signal and slot capability to a QGraphicsEllipseItem. I read that I should inherit from QObject as well, but it didn't work. This code:



class ComState : public QGraphicsEllipseItem, QObject
{
Q_OBJECT

double comRadius;

public:
ComState(QGraphicsItem *parent = 0);
~ComState(){};

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

signals:
void stateChangedOut();
};

ComState::ComState(QGraphicsItem *parent)
: QGraphicsEllipseItem(parent),QObject()
{
comRadius = 0.05;

setFlags(QGraphicsItem::ItemIsMovable);
setRect(QRectF(-comRadius, -comRadius, 2.0*comRadius, 2.0*comRadius));
}



Produces this compile error:



debug\moc_ComState.cpp:44: error: 'staticMetaObject' is not a member of 'QGraphicsEllipseItem'
debug\moc_ComState.cpp: In member function 'virtual void* ComState::qt_metacast(const char*)':
debug\moc_ComState.cpp:64: error: 'qt_metacast' is not a member of 'QGraphicsEllipseItem'
debug\moc_ComState.cpp: In member function 'virtual int ComState::qt_metacall(QMetaObject::Call, int, void**)':
debug\moc_ComState.cpp:69: error: 'qt_metacall' is not a member of 'QGraphicsEllipseItem'


I would really like to make this work instead of going the QGraphicsObject way, because I'm using things that QGraphicsEllipseItem provides.
What do I do now?

Thanks
Cruz

^NyAw^
26th June 2014, 15:37
Hi,

Try changing the order of base classes and also use the "public" keyword as I think that the default interpretation is that it have to inherit as private



class ComState : public QObject, public QGraphicsEllipseItem

Cruz
26th June 2014, 16:02
Yes it worked!

anda_skoa
26th June 2014, 16:51
The orignal code also had the problem that the inheritance from QObject was private.

^NyAw^'s suggestion fixed both problems :)

Cheers,
_