用虚线笔动画QGraphicsItem

时间:2010-12-12 14:28:07

标签: qt animation qt4

我想使用图形视图框架创建一个用Qt:DotLine笔绘制的旋转圆。使用QGraphicsItemAnimation,我可以旋转其他形状,但不能旋转圆圈。下面的程序演示了这个问题:不是矩形和圆圈一起旋转,圆圈在矩形正常旋转时抖动。

alt text

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsItem> 
#include <QTimeLine>
#include <QGraphicsItemAnimation>

QRectF rect (int r)
{
    return QRectF (-r, -r, r * 2, r * 2);
}

void setupRot (QTimeLine *timeline, QGraphicsItem *item)
{
    QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
    animation->setItem(item);
    animation->setTimeLine(timeline);
    animation->setRotationAt (1, 360);

    QObject::connect (timeline, SIGNAL(finished()), animation, SLOT(deleteLater()));
}

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

    QGraphicsScene scene;

    QTimeLine *timeline = new QTimeLine;    
    timeline->setDuration (3000);
    timeline->setCurveShape (QTimeLine::LinearCurve);
    QObject::connect (timeline, SIGNAL(finished()), timeline, SLOT(deleteLater()));

    setupRot (timeline, scene.addEllipse (rect (50), QPen (QBrush (QColor ("blue")), 8, Qt::DotLine)));
    setupRot (timeline, scene.addRect (rect (60)));
    scene.addEllipse (rect (40), QPen (QBrush (QColor ("red")), 8));    

    scene.setSceneRect (-100, -100, 200, 200);    
    QGraphicsView view (&scene);    
    view.show ();    
    timeline->setLoopCount (0);
    timeline->start(); 
    return app.exec ();
}

p.s。:我在网上找到了一些示例代码,用户手动创建中间动画步骤,如下所示:

const int steps = 100;
for (int i = 0; i < steps; ++i)
    animation->setRotationAt (i / (float)steps, 360 / (float)steps * i);

这只是人们不理解插值概念的标志,还是设置(看似多余的)控制点有一些优势?

1 个答案:

答案 0 :(得分:0)

哪个版本/平台?如果我按原样运行代码(或减慢2倍),则虚线圆圈旋转看起来与使用Qt 4.7的Windows中的矩形一样好。