2010-12-12 57 views
1

Je voudrais créer un cercle tournant dessiné avec un stylo Qt:DotLine, en utilisant le Graphics View Framework. En utilisant QGraphicsItemAnimation, je peux faire pivoter d'autres formes mais pas le cercle. Le programme ci-dessous illustre le problème: à la place du rectangle et du cercle qui tournent ensemble, le cercle se déplace alors que le rectangle tourne gracieusement.QGraphicsItem animé avec un pointillé

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

ps: J'ai trouvé quelques exemples de code sur le Web où les gens créent des étapes d'animation intermédiaires manuellement, comme ceci:

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

Est-ce juste un signe de personnes ne comprend-il pas le concept d'interpolation, ou y a-t-il un avantage à établir des points de contrôle (apparemment superflus)?

Répondre

0

Quelle version/plateforme? Si je cours votre code tel quel (ou ralentis 2x), la rotation du cercle en pointillé semble aussi bonne que le rectangle dans Windows avec Qt 4.7.

+0

Ceci est sous Linux/X11 avec QT 4.7.0. – Cactus