Qt 定时器Timer使用
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 | #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(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
2011-04-12 C语言实现二维数组操作--元素个数确定
2011-04-12 Linux下文件搜索、处理实例心得
2011-04-12 Linux Eclipse安装和配置命令行(jre、jdk)