Hi all
I have a class which is drived from QAbstractAnimation and works as i expected. This class moves a text on a line. I want add QState to the code, so when the text starts the movement its state become start and when reaches the end of line, the state is changed to end.
I wrote the following code, but it doesn't work. Can you help me?
Qt Code:
  1. class LineAnimator : public QAbstractAnimation
  2. {
  3. public:
  4. LineAnimator(QPointF start, QPointF end, QGraphicsItem* shape, QObject* parent=0) :
  5. QAbstractAnimation(parent), mShape(shape)
  6. {
  7. path.moveTo(start);
  8. path.lineTo(end);
  9. }
  10.  
  11. virtual int duration() const { return 1000; }
  12.  
  13. virtual void updateCurrentTime(int currentTime)
  14. {
  15. mShape->setPos( path.pointAtPercent(qreal(currentTime) / 1000) );
  16. }
  17.  
  18. private:
  19. QGraphicsItem* mShape;
  20. };
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24. QApplication a(argc, argv);
  25.  
  26. QGraphicsScene* scene = new QGraphicsScene(0, 0, 650, 400);
  27. QGraphicsView *view = new QGraphicsView(scene);
  28.  
  29. QGrapgicsSimpleText* digit_1 = new QGrapgicsSimpleText;
  30. digit_1->setText(QString::number(1));
  31.  
  32. LineAnimator* lineAnimator = new LineAnimator(QPointF(50, 50),
  33. QPointF(250, 50),
  34. digit_1);
  35. scene->addItem(digit_1);
  36.  
  37. // If i use lineAnimator->start() it move the '1' on a line.
  38.  
  39. QStateMachine* machine = new QStateMachine();
  40. QState* start = new QState(machine);
  41. QState* end = new QState(machine);
  42. machine->setInitialState(start);
  43.  
  44. start->setProperty("pos", QPointF(50, 50));
  45. end->setProperty("pos", QPointF(250, 50));
  46.  
  47. QAbstractTransition* t1 = start->addTransition(start, SIGNAL(entered()), end);
  48. t1->addAnimation(lineAnimator);
  49.  
  50. QAbstractTransition* t2 = end->addTransition(end, SIGNAL(entered()), start);
  51.  
  52. machine->start(); // Don't do anything. Just shows '1' on the screen
  53.  
  54. view->show();
  55.  
  56. return a.exec();
  57. }
To copy to clipboard, switch view to plain text mode 

Thanks in advanced.