Qt学习: QTimerEvent定时器事件的处理程序代码示例
来源:互联网 发布:python课程 编辑:程序博客网 时间:2024/05/21 14:40
重要函数:
1.int startTimer(int); //设置定时器,返回一个ld.
2.int event->timerld(); //返回当前的ld.
3.void killTimer(int); //停止定时器.
首先从Qt设计师中拖拽出三个按钮,由于只是演示定时器事件的使用,所以就没有布局的需要了.
以下是”c.cpp”的代码:
#include "c.h"c::c(QWidget *parent) : QMainWindow(parent){ ui.setupUi(this); //连接信号与槽. connect(ui.startButton, SIGNAL(clicked()), this, SLOT(startTimerSlot())); connect(ui.stopButton, SIGNAL(clicked()), this, SLOT(stopTimerSlot()));}c::~c(){}void c::timerEvent(QTimerEvent*event){ //判断当前定时器对应的是哪个ld. if (event->timerId() == this->m_lamp) { if (this->m_lampStatus == false) { //设置按钮的图标. ui.toolButton->setIcon(QIcon("Icons/lamp.png")); this->m_lampStatus = true; } else { ui.toolButton->setIcon(QIcon("Icons/space.png")); this->m_lampStatus = false; } }}void c::startTimerSlot(){ //设置定时器,返回一个timerld.注意单位为毫秒,1000毫秒等于1秒. this->m_lamp = this->startTimer(1000);}void c::stopTimerSlot(){ //停止定时器. this->killTimer(this->m_lamp);}
以下是”c.h”的代码:
#ifndef C_H#define C_H#include <QtWidgets/QMainWindow>#include "ui_c.h"#include <QLabel>#include <QTimerEvent>class c : public QMainWindow{ Q_OBJECTpublic: c(QWidget *parent = 0); ~c();protected: //这是一个虚函数,从QEvent继承而来. void timerEvent(QTimerEvent*event);private slots: void startTimerSlot(); void stopTimerSlot();private: Ui::cClass ui; int m_lamp; bool m_lampStatus = false;};#endif // C_H
最后是”main.cpp”的代码:
#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); c w; w.show(); return a.exec();}
阅读全文
0 0
- Qt学习: QTimerEvent定时器事件的处理程序代码示例
- 我的QT学习笔记-02-QTimerEvent类定时器
- QT::QTimerEvent
- Qt的事件处理和定时器解析
- Qt学习: QPixmap实现的截屏程序代码示例
- Qt的事件处理和定时器解析(实例解析)
- Qt事件处理(五)——定时器
- QT事件处理,鼠标事件,按键键盘事件,定时器,进度条。
- Qt学习: QCloseEvent关闭事件的使用及代码示例
- QT 事件处理 KeyPressEvent 和 定时器时间 Timer
- Qt Quick事件处理之鼠标、键盘、定时器
- Qt Quick事件处理之鼠标、键盘、定时器
- Qt事件处理的使用方法
- QML事件处理--定时器
- Qt学习笔记(六) 事件处理
- Qt学习笔记(三)事件处理
- QT学习之事件处理(一)
- Qt学习笔记——事件处理
- 谈谈高并发解决方案
- 一位过来ACMer的心得
- TabLayout在activity中
- 2017国庆 雅礼集训 题解合集
- 【LR】1界面
- Qt学习: QTimerEvent定时器事件的处理程序代码示例
- 进程间通信之共享内存
- 链队列--简单操作
- 面试题整理
- mysql与oracle分页查询
- mysql Model
- Tablayout的frament方法
- Android 悬浮编辑框
- Dijkstra 单源最短路