Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
一个简单的代码如下:
#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(0100);

    
// Construct a 5-second timeline with a frame range of 0 - 100
    timeLine = new QTimeLine(5000this);
    timeLine
->setFrameRange(0100);
    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就是一个带有线程的对象,通过设置持续时间和帧数来控制动画
posted on 2011-09-07 20:26 ccsdu2009 阅读(4062) 评论(0)  编辑 收藏 引用 所属分类: QT编程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理