一. QObject定时器
1. 定义
QObject 是 Qt 所有对象的基类,内置底层定时器能力,
QTimer本质就是封装了这套机制。它基于 Qt 事件循环 +QTimerEvent事件,靠重写虚函数timerEvent处理定时回调,不是信号槽Qt。
2. 核心API
- int startTimer(int msec, Qt::TimerType timerType = Qt::CoarseTimer)
- void killTimer(int timerId)
- virtual void timerEvent(QTimerEvent *event)
3. 原理流程
- 在 QObject 子类调用
startTimer→ Qt 向操作系统注册定时器,拿到 timerId- 定时时间到 → OS 通知 Qt,Qt 向这个 QObject 所在线程投递
QTimerEvent事件- 线程事件循环取出事件,调用
timerEvent(event)执行你的代码
4. 代码示例
// .h#include<QObject>#include<QTimerEvent>#include<QDebug>classMyTimerObj:publicQObject{Q_OBJECTpublic:explicitMyTimerObj(QObject*parent=nullptr):QObject(parent){timer1=startTimer(1000);// 1000ms = 1stimer2=startTimer(2000);// 2000ms = 2s}protected:voidtimerEvent(QTimerEvent*event)override{if(event->timerId()==timer1){qDebug()<<"1秒定时器触发";}elseif(event->timerId()==timer2){qDebug()<<"2秒定时器触发";}}private:inttimer1;inttimer2;};二. QTimer定时器
1. 定义
QTimer是 Qt Core 模块里的定时器类,继承QObject,基于 Qt 事件循环,靠信号槽工作,用来周期性或者一次性执行任务,GUI 开发最常用,不需要手动开线程Qt。
2. 工作模式
1. 周期定时器
start 之后每隔 interval 毫秒,持续触发
timeout(),直到调用stop()或者对象销毁。
2. 单次定时器 singleShot
setSingleShot(true),触发一次之后定时器自动停止。也可以直接用静态函数QTimer::singleShot()。
3. 常用函数
| 函数 | 作用 |
|---|---|
start(int msec) | 启动定时器,参数是毫秒;如果已经在运行,等价重启 |
stop() | 停止定时器 |
setInterval(int msec) | 设置定时周期(毫秒) |
isActive() | bool,判断定时器是否正在运行 |
setSingleShot(bool) | 设置是否单次触发 |
remainingTime() | 返回距离下一次超时还剩多少毫秒 |
4. 代码示例
QTimer*timer=newQTimer(this);timer->setSingleShot(true);timer->setInterval(2000);// 2秒后执行一次connect(timer,&QTimer::timeout,this,[](){qDebug()<<"2s延迟执行";});timer->start();三. QT的文件操作
创建一个文件夹编译器
1. 页面介绍
对于一个页面,各部分的含义分别如下:
2. 代码说明
- 在头文件引用使用到的基类:
#include
#include
#include
#include
#include
#include- 如果要定义槽,需要
private slots:
void newActionSlot();
void openActionSlot();
void saveActionSlot();- 定义可被外部使用的函数。
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void keyPressEvent(QKeyEvent *k);
void mousePressEvent(QMouseEvent *m);
#ifndefMAINWINDOW_H#defineMAINWINDOW_H#include<QMainWindow>#include<QFileDialog>#include<QMessageBox>#include<QDebug>#include<QKeyEvent>#include<QMouseEvent>namespaceUi{classMainWindow;}classMainWindow:publicQMainWindow{Q_OBJECTpublic:explicitMainWindow(QWidget*parent=0);~MainWindow();voidkeyPressEvent(QKeyEvent*k);voidmousePressEvent(QMouseEvent*m);privateslots:voidnewActionSlot();voidopenActionSlot();voidsaveActionSlot();private:Ui::MainWindow*ui;};#endif// MAINWINDOW_H常用到的connect函数将信号与槽连接起来:
connect(ui->newAction,&QAction::triggered,this,&MainWindow::newActionSlot);connect(ui->openAction,&QAction::triggered,this,&MainWindow::openActionSlot);connect(ui->saveAction,&QAction::triggered,this,&MainWindow::saveActionSlot);定义一个槽函数的步骤:
1. 在头文件创造一个槽函数:
2. 在C源文件中写函数的具体实现:
3. 在起始函数连接信号与槽
四. 事件实现文件保存
常见的QT事件类型如下:
- 键盘事件:按键按下和松开
- 鼠标事件:鼠标移动,鼠标按键的按下和松开
- 拖放事件:用鼠标进行拖放
- 滚轮事件:鼠标滚轮滚动
- 绘屏事件:重绘屏幕的某些部分
- 定时事件:定时器到时
- 焦点事件:键盘焦点移动
- 进入和离开事件:鼠标移入widget之内,或是移出
- 移动事件:widget的位置改变
- 大小改变事件:widget的大小改变
- 显示和隐藏事件:widget显示和隐藏
- 窗口事件:窗口是否为当前窗口
通过重写event函数实现:
voidkeyPressEvent(QKeyEvent*k);voidmousePressEvent(QMouseEvent*m);具体的函数实现:
1. ctrl+s: 实现保存
voidMainWindow::keyPressEvent(QKeyEvent*k){if(k->modifiers()==Qt::ControlModifier&&k->key()==Qt::Key_S){saveActionSlot();}}检测鼠标被按下的效果:
voidMainWindow::mousePressEvent(QMouseEvent*m){QPoint pt=m->pos();qDebug()<<pt;if(m->button()==Qt::LeftButton){qDebug()<<"左键被按下";}elseif(m->button()==Qt::RightButton){qDebug()<<"右键被按下";}}