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"

Qt Code:
  1. #include <QTimer>
  2. #include <QObject>
  3. #include <QGraphicsObject>
  4.  
  5. #include "scribblearea.h"
  6.  
  7. QT_BEGIN_NAMESPACE
  8. class QPixmap;
  9. class QTextEdit;
  10. class QMenu;
  11. class QPainter;
  12. class QWidget;
  13. class QPolygonF;
  14. QT_END_NAMESPACE
  15.  
  16. //! [0]
  17. class DiagramItem : public QObject, public QGraphicsPolygonItem
  18. {
  19. Q_OBJECT
  20. public:
  21. QTimer* iTimer;
  22. QTime* timeValue;
  23. bool zoneState;
  24.  
  25. public:
  26. enum { Type = UserType + 15 };
  27.  
  28. DiagramItem(ScribbleArea *scribble, QGraphicsScene *scene = 0,QGraphicsItem *parent = 0);
  29. QPolygonF polygon() const
  30. { return myPolygon; }
  31.  
  32. int type() const
  33. { return Type;}
  34. void toggleState();
  35. bool getState();
  36.  
  37. /*
  38. public Q_SLOTS:
  39.   void start(int msec);
  40.   void start();
  41.   void stop();
  42.  
  43. public slots:
  44. */
  45.  
  46. private:
  47. QPolygonF myPolygon;
  48.  
  49.  
  50. };
To copy to clipboard, switch view to plain text mode 

Here is "diagramitem.cpp"
Qt Code:
  1. #include <QtGui>
  2. #include <QObject>
  3.  
  4. #include "diagramitem.h"
  5.  
  6.  
  7. //! [0]
  8. DiagramItem::DiagramItem(ScribbleArea *scribble, QGraphicsScene *scene,QGraphicsItem *parent)
  9. : QGraphicsPolygonItem(parent, scene)
  10. {
  11. zoneState = false;
  12. // iTimer = new QTimer();
  13.  
  14. /* QTimer *timer = new QTimer();
  15.   connect(timer, SIGNAL(timeout()), this, SLOT(toggleState()));
  16.   timer->start(1000);
  17. */
  18.  
  19. //QObject::connect(iTimer,SIGNAL(timeout()),scribble,SLOT(timerToggle()));
  20.  
  21. QPolygon p = scribble->getPoly();
  22.  
  23. myPolygon << p.point(0) << p.point(1)<< p.point(2) << p.point(3);
  24.  
  25.  
  26. setPolygon(myPolygon);
  27. setFlag(QGraphicsItem::ItemIsMovable, true);
  28. setFlag(QGraphicsItem::ItemIsSelectable, true);
  29. setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
  30. }
  31.  
  32. //! [5]
  33. void DiagramItem::toggleState()
  34. {
  35. if(zoneState)
  36. zoneState = false;
  37. else
  38. zoneState = true;
  39. }
  40.  
  41. bool DiagramItem::getState()
  42. {
  43. return zoneState;
  44.  
  45. }
To copy to clipboard, switch view to plain text mode