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

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-23 15:21:53

工程代码: 10_timer.rar  

参考资料:


描述:
     使用定时器timer,定时1s ,每一秒时间到了,产生

timeout, 使用 信号槽, 将 label

  1. connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate()));//关联定时器信号与相应的槽函数
实现 label 标签显示 时间



1. 建立工程:







2. 具体代码

在 mianwindow.h中添加
  1. private slots: //add me 槽函数
  2.     void timerUpDate();
在mainwind.cpp中添加

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow) //构造函数
  4. {
  5.     ui->setupUi(this);
  6.     QTimer *timer = new QTimer(this); //新建定时器

  7.     connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate()));//关联定时器信号与相应的槽函数

  8.     timer->start(1000); //定时器开始,初始值1
  9. }

  1. void MainWindow::timerUpDate()
  2. {
  3.     QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
  4.     QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); //设置显示格式
  5.     ui->label->setText(str);//在标签上显示时间
  6. }

显示效果





阅读(541) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~