From: http://dragoon666.blog.163.com/blog/static/107009194201092602326598/

1.新建Gui工程,在主界面上添加一个标签label,并设置其显示内容为“0000-00-00 00:00:00 星期日”。

2.在mainwindow.h中添加槽函数声明。

private slots:

void timerUpDate();

3.在mainwindow.cpp中添加代码。

添加#include <QtCore>的头文件包含,这样就包含了QtCore下的所有文件。

构造函数里添加代码:

QTimer *timer = new QTimer(this);

//新建定时器

connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate()));

//关联定时器计满信号和相应的槽函数

timer->start(1000);

//定时器开始计时,其中1000表示1000ms即1秒

4.然后实现更新函数。

void MainWindow::timerUpDate()

{

QDateTime time = QDateTime::currentDateTime();

//获取系统现在的时间

QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");

//设置系统时间显示格式

ui->label->setText(str);

//在标签上显示时间

}

5.运行程序。

======================================================

以下是本人自己整理的代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow(); private:
void timerEvent(QTimerEvent *); private:
Ui::MainWindow *ui; private slots:
void on_btnLogin_clicked();
#if 0
void timerUpDate();
#endif
}; #endif // MAINWINDOW_H

mainwindow.cpp

#include <QMessageBox>
#include <QtCore>
#include <time.h>
#include "mainwindow.h"
#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if 0
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerUpDate()));
timer->start(1000);
#else
qsrand(time(0));
startTimer(1000); // 返回值为1, 即timerId
startTimer(5000); // 返回值为2
startTimer(10000); // 返回值为3
#endif
} MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::on_btnLogin_clicked()
{
QMessageBox::information(this, "Caption", tr("Hello你好吗"), QMessageBox::Ok);
} #if 0
void MainWindow::timerUpDate()
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->lblCurDate->setText(str);
}
#else
void MainWindow::timerEvent(QTimerEvent *t)
{
switch(t->timerId())
{
case 1:
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->lblCurDate->setText(str);
ui->lbl1->setText(tr("每秒产生一个随机数: %1").arg(qrand() % 10));
ui->lbl1->adjustSize();
}
break;
case 2:
ui->lbl2->setText(tr("5秒后软件将关闭"));
ui->lbl2->adjustSize();
break;
case 3:
qApp->quit(); // 退出系统
break;
}
} #endif

main.cpp

#include <QtGui/QApplication>
#include <QTextCodec>
#include "mainwindow.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
MainWindow w;
w.show(); return a.exec();
}

  

Qt 定时器Timer使用的更多相关文章

  1. QT定时器的两种应用(QObject就有timerEvent事件,一种什么样的居心呢?)

    QT中定时器的使用方法(1)重载timerEvent(QTimerEvent *)函数,然后再在类的构造函数中设置时间间隔   startTimer(50);//单位为毫秒(2)在类的构造函数中设定如 ...

  2. python中实现定时器Timer

    实现定时器最简单的办法是就是循环中间嵌time.sleep(seconds), 这里我就不赘述了 # encoding: UTF-8 import threading #Timer(定时器)是Thre ...

  3. 订餐系统之定时器Timer不定时

    经过几天漫长的问题分析.处理.测试.验证,定时器Timer终于定时了,于是开始了这篇文章,希望对还在纠结于“定时器Timer不定时”的同学有所帮助,现在的方案,在系统日志中会有警告,如果您有更好的方案 ...

  4. PHP框架Swoole的一个定时器Timer特性

    在各种业务型系统中,往往需要服务器在后台扫描相关数据,触发相应的统计.通知等操作. 比如对于一个项目管理系统,需要每天的特定时间内,统计每项任务的执行.到期情况.整个项目的进度等等,根据统计情况,做相 ...

  5. C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)

       功能说明:C#创建一个windows服务,服务启动时D:\mcWindowsService.txt写入数据,服务运行期间每隔两秒写入当前时间. 原理这些就不说了,三语两语说不清楚,直接贴一个实例 ...

  6. iOS 定时器Timer常见问题

    最近有朋友问我使用NStimer遇见与ScrollView并存时存在主线程阻塞的问题,自己总结几种解决方法: 问题原因: 一般定时器timer都会被以默认模式default添加到主线程的runloop ...

  7. 定时器Timer不定时

    订餐系统之定时器Timer不定时 经过几天漫长的问题分析.处理.测试.验证,定时器Timer终于定时了,于是开始了这篇文章,希望对还在纠结于“定时器Timer不定时”的同学有所帮助,现在的方案,在系统 ...

  8. 多线程&定时器Timer&同步&线程通信&ThreadLocal

    1.多线程 线程状态分为:新建状态.就绪状态.运行状态.阻塞状态.死亡状态 对象等待池的阻塞状态:运行状态执行了wait方法 对向锁池的阻塞状态:试图获得某个同步锁,已经被其他线程占用,就会放到对象的 ...

  9. Java进阶(十八)Java实现定时器(Timer)

    Java实现定时器(Timer) 绪 在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等.对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类.java.u ...

随机推荐

  1. 黑客编程教程(十三)多线程DOS程序

    DOS基本原理相信大家都已经很熟悉了,DOS工具大家也用的很熟悉.在群里 经常有人说什么时候去DOS什么东西. 现在我们就自己编写一个DOS工具. #include <winsock2.h> ...

  2. 基于S3C2440的U-BOOT的start.S分析

    基于S3C2440的U-BOOT的start.S分析 在了解了ARM相关的汇编指令后,同时结合网上各位大虾的提点开始阅读u-boot的启动代码,现将分析过程记录如下 可执行文件及内存映射 我们可以把可 ...

  3. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  4. wpf/Silverlight/wp中如何绑定模板中的属性

    <Style TargetType="{x:Type TabItem}" x:Key="EditorTabItemStyle"> <Sette ...

  5. 更改nginx站点根文件夹

    默认站点根文件夹为/usr/local/nginx/html.要将它改成/homw/www vi /usr/local/nginx/conf/nginx.conf 将当中的         locat ...

  6. php底层HashTable的实现

    本文转载自:  http://segmentfault.com/blog/tree/1190000000718519 HashTable对PHP来说是一种非常重要的数据结构.很多PHP的内部实现(变量 ...

  7. 【Unity笔记】物体朝着鼠标位置移动

    其实思路也很简单,就是先获取到鼠标的坐标,然后赋值给目标物体即可. void Update(){ // 物体跟随鼠标移动 Vector2 mousePos = Input.mousePosition; ...

  8. pthread_testcancel和pthread_cancel函数的简单示例

    /*0.取消线程 int pthread_cancel(pthread_t thread); 设置取消点 void pthread_testcancel(void); 测试是否接收到取消请求,如果有, ...

  9. EFM32 DMA/PRS例程

    /**************************************************************************//**  * @file  * @brief H ...

  10. 【mybase】绿色版---mybase非常好用的笔记软件

    mybase绿色软件,体积小,解压可以直接使用,PC端非常好用的笔记软件. 包含两个版本: mybase6.5.0 mybase7.0.0 软件下载地址: 链接: http://pan.baidu.c ...