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类的使用方法。

相关推荐
byxdaz5 小时前
QT中服务使用
开发语言·qt
秋田君13 小时前
QT_网络编程体系
网络·arm开发·qt
Quz14 小时前
QML TextArea 实战(下):大文本加载与性能优化
qt
Quz14 小时前
QML TextArea 实战(中):Markdown 渲染与保持滚动
qt
C++ 老炮儿的技术栈1 天前
基于Qt实现轻量化本地音乐播放器
开发语言·c++·qt·c·播放器·音乐
我不是疯子是傻子1 天前
Qt 实时曲线卡顿优化:从QPainter到OpenGL的3级加速实战
开发语言·qt
秋田君1 天前
QT_XML文件操作
xml·数据库·qt
我不是疯子是傻子1 天前
串口通信数据帧粘包拆包再进化:基于 Qt 的滑动窗口与 CRC 校验实战
开发语言·qt
程序员老陆1 天前
Qt中实现窗口真全屏(覆盖Windows任务栏)
开发语言·windows·qt
CodeKwang2 天前
【三维TSP可视化】1000个点以内的最优路径:最近邻 + 2-opt + 3-opt 组合算法实战(Qt实现)
qt·算法