Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4242365
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-23 12:07:38

工程代码: timer_show.rar  

http://www.cnblogs.com/hnrainll/archive/2011/05/30/2063073.html


Qt4.7.3

1. 建立工程



2. 修改 添加



3. 代码

mainwindow.h 修改
  1. class MainWindow : public QMainWindow
  2. {
  3.     Q_OBJECT
  4. public:
  5.     enum ScreenOrientation {
  6.         ScreenOrientationLockPortrait,
  7.         ScreenOrientationLockLandscape,
  8.         ScreenOrientationAuto
  9.     };

  10.     explicit MainWindow(QWidget *parent = 0);
  11.     virtual ~MainWindow();

  12.     // Note that this will only have an effect on Symbian and Fremantle.
  13.     void setOrientation(ScreenOrientation orientation);

  14.     void showExpanded();

  15. private:
  16.     Ui::MainWindow *ui;
  17.     int num; //声明数码管初始化值对象
  18.     QTimer *timer;

  19. public slots:
  20.     void mySlotStart(); //声明三个公共槽
  21.     void mySlotCount();
  22.     void mySlotStop();
  23. };
mainwindow.cpp 修改

  1. MainWindow::MainWindow(QWidget *parent)
  2.     : QMainWindow(parent), ui(new Ui::MainWindow)
  3. {
  4.     ui->setupUi(this);
  5.     ui->lcdNumber->display(9);//数码管显示9
  6.     timer = new QTimer(this); //为timer 对象创建爱空间
  7.     num = 9;
  8.     //点击start按键时响应mySlotStart槽
  9.     connect(ui->start,SIGNAL(clicked()),this,SLOT(mySlotStart()));
  10.     //timer可以用于定时,当时间到,就会发送一个timerout()的信号,这个是循环的
  11.     //当timerout()信号发出后响应mySlotCount()
  12.     connect(timer,SIGNAL(timeout()),this,SLOT(mySlotCount()));
  13.     //点击stop按键时响应myslotstop槽
  14.     connect(ui->stop,SIGNAL(clicked()),this,SLOT(mySlotStop()));
  15. }

  16. MainWindow::~MainWindow()
  17. {
  18.     delete ui;
  19.     delete timer;//add me
  20. }


  21. void MainWindow::mySlotStart()
  22. {
  23.     //定时器开始工作,初始值为1000
  24.     timer->start(1000);
  25. }

  26. void MainWindow::mySlotCount()
  27. {
  28.     //num自减
  29.     num--;
  30.     if(num <= 0)
  31.     { //num减到0以后
  32.         timer->stop(); //定时器 停止计时
  33.         QLabel *label = new QLabel(this);//创建label
  34.         ui->gridLayout->addWidget(label,0,0,2,2); //让label放在布局中,沾满整个窗口
  35.         label->setScaledContents(true);//图片自适放大
  36.         QMovie *movie = new QMovie("./test.gif");
  37.         label->setMovie(movie);//播放gif图片
  38.         movie->start();//让label显示
  39.         label->show();

  40.     }
  41.     //数据显示在数码管上
  42.     ui->lcdNumber->display(num);
  43. }

  44. void MainWindow::mySlotStop()
  45. {
  46.     //定时器暂停工作
  47.     timer->stop();
  48. }

显示效果





阅读(485) | 评论(0) | 转发(1) |
0

上一篇:Qt paintevent事件

下一篇:Qt 主要的类

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