PDA

View Full Version : Multiple Inheritance problems when inheriting from QGraphicsPolygonItem and QObject



MercyYuen
9th October 2012, 19:15
I have a QGraphicsViewScene that displays a few QGraphicsPolygonItems. I created a class called DiagramItem that is based on QgraphicsPolygonItems. I wanted each polygon item to toggle it's "state" every 10 seconds after it was created. So I added a "state" member to my DiagramItem class called "zoneState."

I saw that for a QTimer to work, it had to be a QObject. So I added QObject as one of the base classes and added the macro Q_OBJECT to the header class. Now it fails to compile. It gives me the error: "undefined reference to 'vtable for DiagramItem.' What is going on? It seems like it should be something pretty basic. Please advise!




Here is my header file "diagramitem.h"


#include <QTimer>
#include <QObject>
#include <QGraphicsObject>

#include "scribblearea.h"

QT_BEGIN_NAMESPACE
class QPixmap;
class QGraphicsItem;
class QGraphicsScene;
class QTextEdit;
class QGraphicsSceneMouseEvent;
class QMenu;
class QGraphicsSceneContextMenuEvent;
class QPainter;
class QStyleOptionGraphicsItem;
class QWidget;
class QPolygonF;
QT_END_NAMESPACE

//! [0]
class DiagramItem : public QObject, public QGraphicsPolygonItem
{
Q_OBJECT
public:
QTimer* iTimer;
QTime* timeValue;
bool zoneState;

public:
enum { Type = UserType + 15 };

DiagramItem(ScribbleArea *scribble, QGraphicsScene *scene = 0,QGraphicsItem *parent = 0);
QPolygonF polygon() const
{ return myPolygon; }

int type() const
{ return Type;}
void toggleState();
bool getState();

/*
public Q_SLOTS:
void start(int msec);
void start();
void stop();

public slots:
*/

private:
QPolygonF myPolygon;


};

Here is "diagramitem.cpp"

#include <QtGui>
#include <QObject>

#include "diagramitem.h"


//! [0]
DiagramItem::DiagramItem(ScribbleArea *scribble, QGraphicsScene *scene,QGraphicsItem *parent)
: QGraphicsPolygonItem(parent, scene)
{
zoneState = false;
// iTimer = new QTimer();

/* QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(toggleState()));
timer->start(1000);
*/

//QObject::connect(iTimer,SIGNAL(timeout()),scribble ,SLOT(timerToggle()));

QPolygon p = scribble->getPoly();

myPolygon << p.point(0) << p.point(1)<< p.point(2) << p.point(3);


setPolygon(myPolygon);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}

//! [5]
void DiagramItem::toggleState()
{
if(zoneState)
zoneState = false;
else
zoneState = true;
}

bool DiagramItem::getState()
{
return zoneState;

}

wysota
9th October 2012, 19:31
Rerun qmake. You need to do that each time you add the Q_OBJECT macro to an existing file.

MercyYuen
9th October 2012, 20:26
Thank you, that worked. For further reading fun and limitations regarding Meta Object Compiler go here: http://doc.qt.digia.com/4.2/moc.html#limitations