PDA

View Full Version : Add animation between two QStates



ehsanTC
23rd September 2013, 09:28
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?


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;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

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

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

LineAnimator* lineAnimator = new LineAnimator(QPointF(50, 50),
QPointF(250, 50),
digit_1);
scene->addItem(digit_1);

// If i use lineAnimator->start() it move the '1' on a line.

QStateMachine* machine = new QStateMachine();
QState* start = new QState(machine);
QState* end = new QState(machine);
machine->setInitialState(start);

start->setProperty("pos", QPointF(50, 50));
end->setProperty("pos", QPointF(250, 50));

QAbstractTransition* t1 = start->addTransition(start, SIGNAL(entered()), end);
t1->addAnimation(lineAnimator);

QAbstractTransition* t2 = end->addTransition(end, SIGNAL(entered()), start);

machine->start(); // Don't do anything. Just shows '1' on the screen

view->show();

return a.exec();
}


Thanks in advanced.