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.