Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1611892
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: C/C++

2009-04-16 21:30:11

在练习qt中时间类的使用,下边派生类,该类会显示变色的圆圈,可以通过传入参数设计定圆圈的颜色,同时,类对象有生存期,时间超时后,对象消亡。
 

#ifndef MOVIE_H
#define MOVIE_H
#include <QWidget>
#include <QTimer>
#include <QPaintEvent>
#include <QTimer>
#include <QPainter>
class movie:public QWidget
{
    Q_OBJECT
    public:
        movie(QWidget *parent=0);
        static int movie_num;
    private:
        int movie_times;
        int wait_sec;
        int tmp;
        QPainter *painter;
        QTimer *wait_timer,*flash_timer;
    protected:    
        void paintEvent(QPaintEvent *event);
    public slots:
        void stop(void);
        void start(void);
    signals:
        void timeout(void);        
};

#endif


#include "movie.h"
#include <QWidget>
#include <QTimer>
#include <QPainter>
int movie_num=0;
movie::movie(QWidget *parent):QWidget(parent)
{
    tmp=0;
    wait_sec=10;
    movie_times=500;
    wait_timer=new QTimer(this);
    flash_timer=new QTimer(this);
    wait_timer->start(wait_sec*1000);
    flash_timer->start(movie_times);
    connect(flash_timer,SIGNAL(timeout()),this,SLOT(update()));
    connect(wait_timer,SIGNAL(timeout()),this,SLOT(close()));
}    

void movie::stop(void)
{
    flash_timer->stop();
    wait_timer->stop();
    emit this->timeout();
}    

void movie::start(void)
{
    flash_timer->start();
    wait_timer->start();    
}    

void movie::paintEvent(QPaintEvent *)
{
    
    painter=new QPainter(this);
    if((tmp++)==0)
    {
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::blue);
        painter->drawEllipse(10,10,40,40);
    }    
    else
    {
        tmp=0;
        painter->setPen(Qt::NoPen);
        painter->setBrush(Qt::red);
        painter->drawEllipse(10,10,40,40);
        
    }
    delete painter;
}    

阅读(2188) | 评论(0) | 转发(0) |
0

上一篇:QT入门例程【2】

下一篇:qt学习感受

给主人留下些什么吧!~~