1

我有一个类可以成功地为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 的QState的类,当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();

谢谢。

4

0 回答 0