1
我想用Qt:DotLine
筆使用圖形視圖框架創建一個旋轉圓。使用QGraphicsItemAnimation
,我可以旋轉其他形狀而不是圓形。下面的程序演示了這個問題:矩形和圓形一起旋轉,而矩形旋轉時,圓形會左右搖晃。帶虛線筆的動畫QGraphicsItem
#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:我發現那裏的人都手動創建的中間步驟的動畫在網絡上一些示例代碼,就像這樣:
const int steps = 100;
for (int i = 0; i < steps; ++i)
animation->setRotationAt (i/(float)steps, 360/(float)steps * i);
這只是人的一個標誌不理解插值的概念,還是設置(看似多餘的)控制點有一些優勢?
這是在Linux/X11與QT 4.7.0。 – Cactus 2010-12-14 16:50:42