开发者

Animated QGraphicsItem with dashed pen

开发者 https://www.devze.com 2023-01-30 08:22 出处:网络
I\'d like to create a rotating circle drawn with a Qt:DotLine pen, using the Graphics View Framework. Using QGraphicsItemAnimation, I can rotate other shapes but not the circle. The program below demo

I'd like to create a rotating circle drawn with a Qt:DotLine pen, using the Graphics View Framework. Using QGraphicsItemAnimation, I can rotate other shapes but not the circle. The program below demonstrates the problem: instead of the rectangle and the circle rotating together, the circle jerks around while the rectangle rotates gracefully.

Animated QGraphicsItem with dashed pen

#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, S开发者_如何学编程IGNAL(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.: I've found some sample code on the web where people are creating intermediate animation steps manually, like this:

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

Is this just a sign of people not understanding the concept of interpolation, or is there some advantage of setting (seemingly superfluous) control points?


Which version/platform? If I run your code as is (or slowed down 2x), the dotted circle rotation looks as good as the rectangle in Windows with Qt 4.7.

0

精彩评论

暂无评论...
验证码 换一张
取 消