1

I have a class that simply animates a QGraphicsItem successfully.

class LineAnimator : public QAbstractAnimation
{
public:
LineAnimator(QPointF start, QPointF end, QGraphicsItem* shape, QObject* parent=0) : 
 QAbstractAnimation(parent), mShape(shape)
{
    path.moveTo(start);
    path.lineTo(end);
}

virtual int duration() const { return 1000; }

virtual void updateCurrentTime(int currentTime)
{
    mShape->setPos( path.pointAtPercent(qreal(currentTime) / 1000) );
}

private:
    QPainterPath path;
    QGraphicsItem* mShape;
};

I want to change the class that I can add two QStates called s1 and s2 in a way that when QGraphicsItem is not moving, its state is s1. While it changes its state to s2, it should move on my defined animation class. At the end it must change its state to s2. I tried to implement this by below code, but it doesn't work. Can you explain why?

QGraphicsScene* scene = new QGraphicsScene(0, 0, 650, 400);
QGraphicsView *view = new QGraphicsView(scene);

QGrapgicsSimpleText* digit_1 = new QGrapgicsSimpleText;
digit_1->setText(QString::number(1));
scene->addItem(digit_1);

LineAnimator* lineAnimator = new LineAnimator(QPointF(50, 50),
                                          QPointF(250, 50),
                                          digit_1);
//-----------------------------
lineAnimator->setLoopCount(2);
//-----------------------------

QLable* lbl = new QLabel("State machine is not started.");

QStateMachine* machine = new QStateMachine();
QState*             s1 = new QState(machine);
QState*             s2 = new QState(machine);

machine->addState(s1);
machine->addState(s2);
machine->setInitialState(s1);

s1->assignProperty(lbl, "text", "State 1");
s2->assignProperty(lbl, "text", "State 2");

QAbstractTransition* t1 = s1->addTransition(lineAnimator, SIGNAL(finished()), s2);
t1->addAnimation(lineAnimator);

lbl->show();
machine->start();
lineAnimator->stat();

view->show();

Thanks.

Ehsan Toghian
  • 548
  • 1
  • 6
  • 26
  • I cannot find when and how your `LineAnimator::start()` called. – vahancho Sep 24 '13 at 11:53
  • @vahancho, if I add a new line like `lineAnimator->start()` before the comment about `LineAnimator::start()` and comment the rest, the **1** would move as expected. But the above code do nothing that showing 1. – Ehsan Toghian Sep 24 '13 at 13:00
  • 1
    @ ehsan, I see, but my question was how it should move if you do not start animation? You only start your state machine, but how the animation will be started? – vahancho Sep 24 '13 at 13:06
  • @vahancho, thanks for your tip. I could fix the animation and states by the new posted code. But another question, in the line `lineAnimator->setLoopCount(2);` when the whole loops terminates, the state s1 changes to s2. Consequently, just in the start of animation and at the end i have state changes. How can i change the code in a way that when every single animation's loop is finished, the state changes to s2 and when animation starts again it changes to s1? – Ehsan Toghian Sep 25 '13 at 06:18
  • Is there a way that when the `digit_1` is moving on the line, it rotates 360 degrees? – Ehsan Toghian Oct 03 '13 at 11:42

0 Answers0