QTimer类的使用方法

本文介绍QTimer类的使用方法。

1.单次触发

在某些情况下,定时器只运行一次,可使用单次触发方式。

cpp 复制代码
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::timeout);
timer->setSingleShot(true);
timer->start(1000);

void MainWindow::timeout()
{



}

或直接采用静态方法:

cpp 复制代码
QTimer::singleShot(1000, this, &MainWindow::timeout);

void MainWindow::timeout()
{



}

2.重复触发

定时器需要重复触发,可采用重复触发方式。

cpp 复制代码
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::timeout);
timer->start(1000);

timer->stop();  //stop timer

void MainWindow::timeout()
{



}

总结,本文介绍了Qt编程中QTimer类的使用方法。

相关推荐
赵民勇6 小时前
Qt QML Component.onCompleted 和 Component.onDestruction 详解
qt
我不是8神6 小时前
Qt 知识点全面总结
开发语言·qt
Lhan.zzZ10 小时前
基于Qt的UDP广播发现与TCP连接系统的设计与实现
qt·tcp/ip·udp
leiming611 小时前
c++ QT 开发第二天,用ui按钮点亮实体led
开发语言·qt·ui
hqwest11 小时前
码上通QT实战04--主窗体布局
开发语言·css·qt·布局·widget·layout·label
leiming611 小时前
c++ qt开发第一天 hello world
开发语言·c++·qt
赵民勇13 小时前
QML Base Type 详解
qt
hqwest13 小时前
码上通QT实战07--主窗体消息栏设计
开发语言·qt·qt事件·主窗体·stackedwidget
hqwest13 小时前
码上通QT实战06--导航按钮事件
开发语言·qt·mousepressevent·qfont·qpainter·qlineargradient·setbrush
CC.GG14 小时前
【Qt】常用控件----容器类控件(QGroupBox、QTabWidget )以及布局管理器
开发语言·qt