#include <QtGui/QApplication> #include <QtGui>
#include <math.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsItem *ball = new QGraphicsEllipseItem(0,0,20,40);
QTimeLine *timer = new QTimeLine(15000);
timer->setFrameRange(0,100);
QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
animation->setItem(ball);
animation->setTimeLine(timer);
for(int i = 0; i < 600; ++i)
animation->setPosAt(i / 600.0, QPointF(i, i));
QGraphicsScene *scene = new QGraphicsScene();
scene->setBackgroundBrush(QBrush(Qt::blue));
scene->setSceneRect(0,0,250,250);
scene->addItem(ball);
QGraphicsView *view = new QGraphicsView(scene);
view->setBackgroundBrush(QBrush(Qt::red));
view->show();
timer->start();
return a.exec();
}
另外一个例子如下:
#ifndef TIMELINE_H
#define TIMELINE_H
#include <QtGui/QWidget>
#include <QVBoxLayout>
#include <QTimeLine>
#include <QProgressBar>
#include <QPushButton>
class timeline : public QWidget
{
Q_OBJECT
public:
timeline(QWidget *parent = 0);
~timeline();
private:
QPushButton *pushButton;
QTimeLine *timeLine;
QProgressBar *progressBar;
QVBoxLayout *layout;
};
#endif // TIMELINE_H
#include "timeline.h"
timeline::timeline(QWidget *parent):QWidget(parent)
{
layout= new QVBoxLayout(this);
progressBar = new QProgressBar(this);
progressBar->setRange(0, 100);
// Construct a 5-second timeline with a frame range of 0 - 100
timeLine = new QTimeLine(5000, this);
timeLine->setFrameRange(0, 100);
connect(timeLine, SIGNAL(frameChanged(int)), progressBar, SLOT(setValue(int)));
// Clicking the push button will start the progress bar animation
pushButton = new QPushButton(tr("Start animation"), this);
connect(pushButton, SIGNAL(clicked()), timeLine, SLOT(start()));
layout->addWidget(progressBar);
layout->addWidget(pushButton);
setLayout(layout);
}
timeline::~timeline()
{
// No need to delete any QObject that got proper parent pointer.
}
简单的说 QTimeLine就是一个带有线程的对象,通过设置持续时间和帧数来控制动画