QT学习笔记-24.使用QTimeLine

一个简单的代码如下:

#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就是一个带有线程的对象,通过设置持续时间和帧数来控制动画



FROM: http://www.cppblog.com/gaimor/archive/2011/09/07/155302.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值