在练习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; }
|
阅读(2211) | 评论(0) | 转发(0) |