恒美微站
首页
关于我们
建站服务
主题模板
案例展示
资讯中心
联系我们
【Qt学习笔记】Qt进阶操作功能(三)
首页
资讯中心
/
【Qt学习笔记】Qt进阶操作功能(三)
【Qt学习笔记】Qt进阶操作功能(三)
发布时间:2026/9/14 22:49:36
一. QObject定时器1. 定义QObject 是 Qt 所有对象的基类内置底层定时器能力QTimer本质就是封装了这套机制。它基于 Qt 事件循环 QTimerEvent事件靠重写虚函数timerEvent处理定时回调不是信号槽Qt。2. 核心APIint startTimer(int msec, Qt::TimerType timerType Qt::CoarseTimer)void killTimer(int timerId)virtual void timerEvent(QTimerEvent *event)3. 原理流程在 QObject 子类调用startTimer→ Qt 向操作系统注册定时器拿到 timerId定时时间到 → OS 通知 QtQt 向这个 QObject 所在线程投递QTimerEvent事件线程事件循环取出事件调用timerEvent(event)执行你的代码4. 代码示例// .h#includeQObject#includeQTimerEvent#includeQDebugclassMyTimerObj:publicQObject{Q_OBJECTpublic:explicitMyTimerObj(QObject*parentnullptr):QObject(parent){timer1startTimer(1000);// 1000ms 1stimer2startTimer(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. 单次定时器 singleShotsetSingleShot(true)触发一次之后定时器自动停止。也可以直接用静态函数QTimer::singleShot()。3. 常用函数函数作用start(int msec)启动定时器参数是毫秒如果已经在运行等价重启stop()停止定时器setInterval(int msec)设置定时周期毫秒isActive()bool判断定时器是否正在运行setSingleShot(bool)设置是否单次触发remainingTime()返回距离下一次超时还剩多少毫秒4. 代码示例QTimer*timernewQTimer(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#includeQMainWindow#includeQFileDialog#includeQMessageBox#includeQDebug#includeQKeyEvent#includeQMouseEventnamespaceUi{classMainWindow;}classMainWindow:publicQMainWindow{Q_OBJECTpublic:explicitMainWindow(QWidget*parent0);~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. ctrls: 实现保存voidMainWindow::keyPressEvent(QKeyEvent*k){if(k-modifiers()Qt::ControlModifierk-key()Qt::Key_S){saveActionSlot();}}检测鼠标被按下的效果voidMainWindow::mousePressEvent(QMouseEvent*m){QPoint ptm-pos();qDebug()pt;if(m-button()Qt::LeftButton){qDebug()左键被按下;}elseif(m-button()Qt::RightButton){qDebug()右键被按下;}}五. TCP客户端