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. Windows上怎么安装ELK

    In this guide I will show that it is also possible to run Logstash on a Windows Server 2012 machine ...

  2. hadoop详细了解5个进程的作用

    1.job的本质是什么?2.任务的本质是什么?3.文件系统的Namespace由谁来管理,Namespace的作用是什么?4.Namespace 镜像文件(Namespace image)和操作日志文 ...

  3. Android: TODO 应用交互的两种实现方法(Behavior)

    最近在写 TODO app,涉及到 Calendar 和 RecyclerView 的交互, 需求: 1. 往上滑动, Calendar 显示为周 2. 周显示模式下,往下滑动,显示为月 3. 列表下 ...

  4. 批处理学习笔记14 - 把所有.mp4文件全部拷贝进统一目录

    今天下载了一套视频教程,结果发现不在同一个目录下,很乱.都放在不同文件夹下. 于是写了一个批处理来解决这个问题 @echo off for /r %%i in (*mp4) do ( copy %%i ...

  5. c# 获取当前程序运行根目录

    //获取绝对路径,调用如 string fileName = string.Format("~/RuleConfigFiles/Campaign_{0}.JSON", Campai ...

  6. The DiskSpd Storage Performance Tool

    https://enterpriseitnotes.wordpress.com/2013/05/31/understanding-ios-iops-and-outstanding-ios/ https ...

  7. spring使用rssfeed

    spring使用rssfeed import org.springframework.stereotype.Controller; import org.springframework.web.bin ...

  8. LeetCode: Set Matrix Zeroes 解题报告

    Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...

  9. quartz 实现调度任务 SchedulerManager

    package cn.com.do1.component.common.util; import org.quartz.*; import org.quartz.impl.StdSchedulerFa ...

  10. 一款效果非常不错的jquery插件 -Lightbox

    今天为大家介绍一款非常不错的jquery图片查看插件-Lightbox.这款图片查看插件体验很好,不仅有左右两个小箭头可以浏览上一张和下一张图片,还支持鼠标的左右键浏览上一张和下一张图片.有点类似go ...