qt 线程
qt线程创建有两种方式:
1、信号槽形式
2、继承QThread形式。
这里记一下信号槽形式。参考:https://blog.csdn.net/douzhq/article/details/104156580
worker.h
1 #ifndef WORKER_H 2 #define WORKER_H 3 4 #include <QObject> 5 class Worker : public QObject 6 { 7 Q_OBJECT 8 9 public: 10 Worker(QObject* parent = nullptr); 11 ~Worker(); 12 13 public slots: 14 // 计算前count个数的和 15 void doWork(int count); 16 17 signals: 18 // 发送计算完成信号 19 void doFinished(int); 20 }; 21 22 #endif
worker.cpp
1 #include "Worker.h" 2 #include <QDebug> 3 #include <QThread> 4 5 Worker::Worker(QObject* parent) 6 :QObject(parent) 7 { 8 9 } 10 11 Worker::~Worker() 12 { 13 14 } 15 16 // 计算 0~count个数的和 17 void Worker::doWork(int count) 18 { 19 int sum = 0; 20 for (int i=0; i<=count; ++i) 21 sum += i; 22 23 // 打印当前函数名,线程ID,以及计算结果 24 qDebug() << __FUNCTION__ << "Thread ID: " << QThread::currentThreadId() << ", Result is " << sum; 25 26 emit doFinished(sum); 27 }
controller.h
1 #ifndef MYOBJECT_H 2 #define MYOBJECT_H 3 4 #include <QObject> 5 #include <QThread> 6 7 class Controller : public QObject 8 { 9 Q_OBJECT 10 11 public: 12 Controller(QObject* parent = nullptr); 13 ~Controller(); 14 15 // 开启线程计算 16 void startThreadRunFunc(int number); 17 18 private: 19 QThread m_thread; 20 21 signals: 22 // 该信号用于触发工作者中的槽函数 23 void startCalcSum(int); 24 25 private slots: 26 // 接受计算完毕后的结果槽函数 27 void onCalcSumFinished(int sum); 28 }; 29 30 #endif
controller.cpp
1 #include "Controller.h" 2 #include "Worker.h" 3 #include <QDebug> 4 5 Controller::Controller(QObject* parent) 6 :QObject (parent) 7 { 8 // [1] 9 Worker* worker = new Worker; 10 worker->moveToThread(&m_thread); 11 12 // [2] 13 QObject::connect(this, &Controller::startCalcSum, worker, &Worker::doWork); 14 // [3] 15 QObject::connect(worker, &Worker::doFinished, this, &Controller::onCalcSumFinished); 16 17 // [4] 当线程退出时,释放工作者内存 18 QObject::connect(&m_thread, &QThread::finished, worker, &Worker::deleteLater); 19 20 // [5] 21 m_thread.start(); 22 } 23 24 Controller::~Controller() 25 { 26 m_thread.quit(); 27 m_thread.wait(); 28 } 29 30 void Controller::startThreadRunFunc(int number) 31 { 32 // 发送开始计算信号 33 emit startCalcSum(number); 34 qDebug() << __FUNCTION__ << " : Current Thread is " << QThread::currentThreadId(); 35 } 36 37 void Controller::onCalcSumFinished(int sum) 38 { 39 // 打印行数名,当前线程ID,计算结果 40 qDebug() << __FUNCTION__ \ 41 << " : Current Thread is " << QThread::currentThreadId() \ 42 << ", Result is " << sum; 43 }
调用方法:
#include <QCoreApplication> #include <QThread> #include "Controller.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 创建控制器 Controller *object = new Controller; // 计算前100个数的和 object->startThreadRunFunc(100); return a.exec(); }
执行结果如下:
Controller::startThreadRunFunc : Current Thread is 0x491c
Worker::doWork Thread ID: 0x62c0 , Result is 5050
Controller::onCalcSumFinished : Current Thread is 0x491c , Result is 5050
整体流程如下:
函数 startThreadRunFunc 发送信号 startCalcSum ,此过程在主线程中执行。
触发 doWork 槽函数,计算前100个数的和,并发送信号 doFinished 信号,此过程在新建的线程中执行。
触发 onCalcSumFinished 槽函数,此过程在主线程中执行。