在两个QStates之间添加我自己的动画

时间:2013-09-24 11:43:00

标签: qt animation state qstatemachine

我有一个类,只是简单地动画 QGraphicsItem

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

我想改变我可以添加两个名为s1和s2的 QStates 的类,其方式是当 QGraphicsItem 不移动时,其状态为s1。当它将状态更改为s2时,它应该继续我定义的动画类。最后,它必须将其状态更改为s2。 我尝试通过下面的代码实现这一点,但它不起作用。你能解释一下原因吗?

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();

感谢。

0 个答案:

没有答案