Qt操作Thread类
相关资料:
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 }
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417075318%E5%8A%A0%E6%88%91QQ.jpg)
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417080925%E5%8A%A0%E6%88%91%E5%BE%AE%E4%BF%A1.jpg)