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
QT_END_NAMESPACE
//! [0]
{
Q_OBJECT
public:
bool zoneState;
public:
enum { Type = UserType + 15 };
{ 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:
};
#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;
};
To copy to clipboard, switch view to plain text mode
Here is "diagramitem.cpp"
#include <QtGui>
#include <QObject>
#include "diagramitem.h"
//! [0]
{
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()));
myPolygon << p.point(0) << p.point(1)<< p.point(2) << p.point(3);
setPolygon(myPolygon);
}
//! [5]
void DiagramItem::toggleState()
{
if(zoneState)
zoneState = false;
else
zoneState = true;
}
bool DiagramItem::getState()
{
return zoneState;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks