Qt操作Thread类
Qt 操作 thread
2023-09-14 08:57:11 时间
相关资料:
https://blog.csdn.net/u012635648/article/details/89504115
https://blog.csdn.net/qq_28171461/article/details/90518324
.pro
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 QT += core gui 2 QT += network 3 4 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 5 6 CONFIG += c++11 7 8 # The following define makes your compiler emit warnings if you use 9 # any Qt feature that has been marked deprecated (the exact warnings 10 # depend on your compiler). Please consult the documentation of the 11 # deprecated API in order to know how to port your code away from it. 12 DEFINES += QT_DEPRECATED_WARNINGS 13 14 # You can also make your code fail to compile if it uses deprecated APIs. 15 # In order to do so, uncomment the following line. 16 # You can also select to disable deprecated APIs only up to a certain version of Qt. 17 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 18 19 SOURCES += \ 20 main.cpp \ 21 mainwindow.cpp 22 23 HEADERS += \ 24 mainwindow.h 25 26 FORMS += \ 27 mainwindow.ui 28 29 # Default rules for deployment. 30 qnx: target.path = /tmp/$${TARGET}/bin 31 else: unix:!android: target.path = /opt/$${TARGET}/bin 32 !isEmpty(target.path): INSTALLS += target
main.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QThread> 6 #include <QPainter> 7 #include <QVBoxLayout> 8 #include <QPushButton> 9 #include <QToolTip> 10 #include <QDebug> 11 #include <QTextEdit> 12 13 QT_BEGIN_NAMESPACE 14 namespace Ui { class MainWindow; } 15 QT_END_NAMESPACE 16 17 18 class WorkThread : public QThread 19 { 20 21 public: 22 WorkThread(QTextEdit *textEdit, QObject *parent = nullptr); 23 ~WorkThread(); 24 // 结束线程 25 void stop(); 26 void paused(); 27 void begin(); 28 protected: 29 bool m_stop; 30 bool m_paused; 31 QTextEdit *m_pTextEdit; 32 void run(); 33 }; 34 35 class MainWindow : public QMainWindow 36 { 37 Q_OBJECT 38 39 public: 40 MainWindow(QWidget *parent = nullptr); 41 ~MainWindow(); 42 private slots: 43 void on_pushButton_clicked(); 44 45 void on_pushButton_2_clicked(); 46 47 void on_pushButton_3_clicked(); 48 49 private: 50 Ui::MainWindow *ui; 51 WorkThread *m_pWorkThread; 52 }; 53 #endif // MAINWINDOW_H
mainwindow.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 setWindowTitle(QStringLiteral("Qt操作Thread类")); 10 11 m_pWorkThread = new WorkThread(ui->textEdit); 12 m_pWorkThread->start(); 13 } 14 15 MainWindow::~MainWindow() 16 { 17 delete ui; 18 19 m_pWorkThread->stop(); 20 m_pWorkThread->exit(0); 21 delete m_pWorkThread; 22 } 23 24 void WorkThread::run() 25 { 26 while(!m_stop) 27 { 28 if (false == m_paused) 29 { 30 m_pTextEdit->append(QStringLiteral("进行中...")); 31 } 32 sleep(2); 33 } 34 } 35 36 WorkThread::WorkThread(QTextEdit *textEdit, QObject *parent) 37 :QThread(parent) 38 { 39 m_stop = false; 40 m_paused = false; 41 m_pTextEdit = textEdit; 42 } 43 44 WorkThread::~WorkThread() 45 { 46 // 请求终止 47 requestInterruption(); 48 quit(); 49 wait(); 50 } 51 52 void WorkThread::stop() 53 { 54 m_stop = true; 55 } 56 57 void WorkThread::paused() 58 { 59 m_paused = true; 60 } 61 62 void WorkThread::begin() 63 { 64 m_paused = false; 65 } 66 67 void MainWindow::on_pushButton_clicked() 68 { 69 m_pWorkThread->begin(); 70 } 71 72 void MainWindow::on_pushButton_2_clicked() 73 { 74 m_pWorkThread->paused(); 75 } 76 77 void MainWindow::on_pushButton_3_clicked() 78 { 79 m_pWorkThread->stop(); 80 }
相关文章
- python qt是什么_初识Python与Qt「建议收藏」
- Qt 用QMediaPlayer实现简易播放器
- Qt面试笔试题问答经验总结
- qtcpsocket断开_2020-05-06 QT子线程使用QTcpSocket连接服务器
- PyQt5可视化 7 饼图和柱状图实操案例 ①Qt项目的创建
- C/C++ Qt QThread 线程组件应用
- C/C++ Qt TreeWidget 嵌套节点操作技巧
- Qt官方示例-网络下载器
- 在 Linux 上快速搭建 Qt 运行环境(qt运行环境linux)
- Qt蓝牙编程指南:在Linux系统上使用Qt开发蓝牙应用程序(qt蓝牙编程linux)
- 开发Linux下用Qt开发:简单易用的开发方式(linux用qt)
- Linux下Qt跃动编程之旅(linux下qt串口编程)
- 开发Linux下 Qt开发:从入门到精通.(linux下的qt)
- 基于Linux和Qt打造高效聊天室,让通信更便利(linuxqt聊天室)
- 解决Qt连接MySQL的问题(qt连接不上mysql)
- MySQL操作简单化:Qt实现(qtmysql操作)
- QT快速加载MySQL驱动的简单操作(qt加载mysql驱动)
- QT实现对Oracle数据库的操作教程(qt操作oracle)
- 深入了解Linux QT版本,掌握最新技术动态(linuxqt版本)
- 使用Linux QT轻松操作数据库(linuxqt数据库)
- Qt与SQLServer结合,提升开发效率(qt和sqlserver)
- 界面让Linux拥有Qt完美图形界面的魅力(linux qt 图形)
- 从 Qt 中编译到 MySQL 的进程(qt mysql 编译)