【问题标题】:QGraphicsItem moving along pathQGraphicsItem 沿路径移动
【发布时间】:2012-02-06 08:17:32
【问题描述】:

是否可以为QGraphicsItem 设置动画,使其沿着QGraphicsScene / QGraphicsView 内的某个路径移动?

类似于this demo,其中白点沿齿轮移动 - 一条闭合曲线。

如果是这样,那怎么办?

【问题讨论】:

    标签: qt animation graphics qgraphicsview


    【解决方案1】:

    找到解决方案here

    QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 20, 20);
    
    QTimeLine *timer = new QTimeLine(5000);
    timer->setFrameRange(0, 100);
    
    QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
    animation->setItem(ball);
    animation->setTimeLine(timer);
    
    animation->setPosAt(0.f / 200.f, QPointF(0, 0));
    animation->setPosAt(10.f / 200.f, QPointF(0, 30));
    animation->setPosAt(20.f / 200.f, QPointF(20, 30));
    animation->setPosAt(30.f / 200.f, QPointF(20, 20));
    animation->setPosAt(40.f / 200.f, QPointF(30, 20));
    animation->setPosAt(50.f / 200.f, QPointF(30, 30));
    animation->setPosAt(60.f / 200.f, QPointF(40, 30));
    animation->setPosAt(70.f / 200.f, QPointF(40, 60));
    animation->setPosAt(80.f / 200.f, QPointF(50, 60));
    animation->setPosAt(90.f / 200.f, QPointF(50, 0));
    animation->setPosAt(100.f / 200.f, QPointF(70, 0));
    animation->setPosAt(110.f / 200.f, QPointF(70, 10));
    animation->setPosAt(120.f / 200.f, QPointF(80, 10));
    
    QGraphicsScene *scene = new QGraphicsScene();
    scene->setSceneRect(0, 0, 250, 250);
    scene->addItem(ball);
    
    ui->graphicsView->setScene(scene);
    
    timer->start();
    

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 2011-09-03
      • 2011-05-26
      • 2011-08-20
      • 1970-01-01
      • 2020-05-12
      • 2011-10-02
      • 2011-01-22
      • 1970-01-01
      相关资源
      最近更新 更多