QT線程QThread的使用介紹
1. 概述
QThread 有兩種使用方式
QObject::moveToThread()
- 派生
QThread
的子類類
2. moveThread 示例
步驟概述:
- 定義一個
QObject
派生類,在派生類中定義一個槽函數(shù),此函數(shù)是用于執(zhí)行具體的工作 - 在要使用線程的類中,新建
QThread
和QObject
派生類對象,并使用moveThread()
將派生類的處理交由QThread
- 將觸發(fā)線程工作的信號與派生類的槽函數(shù)進(jìn)行連接
ThreadWorker.hpp
代碼如下:
#ifndef THREADWORKER_HPP #define THREADWORKER_HPP #include <QObject> #include <QString> #include <QThread> #include <QDebug> class ThreadWorker:public QObject { Q_OBJECT public: ThreadWorker() {} public slots: void work(QString p1) { qDebug() << "current thread ID:" << QThread::currentThreadId(); qDebug() << p1; QThread::sleep(10); qDebug() << "thread run finish!"; } }; #endif // THREADWORKER_HPP
ThreadController.hpp
代碼如下:
#ifndef THREADCONTROLLER_H #define THREADCONTROLLER_H #include "ThreadWorker.hpp" class ThreadController:public QObject { Q_OBJECT QThread workerThread; public: ThreadController():QObject() { ThreadWorker* threadWork = new ThreadWorker(); // 將 threadWork 移交給 workerThread threadWork->moveToThread(&workerThread); QObject::connect(this,SIGNAL(touchWork(QString)),threadWork,SLOT(work(QString))); QObject::connect(&workerThread,&QThread::finished,threadWork,&QObject::deleteLater); workerThread.start(); //啟動線程 qDebug()<<"current thread ID:"<<QThread::currentThreadId()<<'\n'; emit touchWork("working"); } ~ThreadController() { workerThread.quit(); workerThread.wait(); } signals: // 發(fā)出信號觸發(fā)線程 void touchWork(QString p1); }; #endif // THREADCONTROLLER_H
main.cpp
代碼如下:
#include <QCoreApplication> #include "ThreadController.hpp" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); ThreadController tc = ThreadController(); return a.exec(); }
注意:
不能再cpp
文件中使用QT
的特性機(jī)制如信號槽,因為moc
不會在cpp
文件中處理這些機(jī)制??梢愿?,但比較麻煩,建議將類定義在頭文件中即可。
3. QThread 示例
方法概述:
- 定義一個
QThread
的派生類,并重載run()
函數(shù),在run()
函數(shù)中寫入具體的線程代碼 - 通過
start()
啟動線程
CustomThread.hpp
代碼如下
#ifndef CUSTOMTHREAD_H #define CUSTOMTHREAD_H #include <QThread> #include <QDebug> class CustomThread:public QThread { Q_OBJECT public: CustomThread() {} signals: void customThreadSignal(); public slots: void customThreadSlot() { qDebug()<<"current thread ID(in slot function):"<<QThread::currentThreadId()<<'\n'; } protected: void run() override { qDebug()<<"current thread ID:"<<QThread::currentThreadId()<<'\n'; QThread::sleep(10); qDebug() << "thread run finish!"; emit customThreadSignal(); } }; #endif // CUSTOMTHREAD_H
main.cpp
代碼如下
#include <QCoreApplication> #include "CustomThread.hpp" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << "main thread ID:" << QThread::currentThreadId(); CustomThread customThread; QObject::connect(&customThread,&CustomThread::customThreadSignal,&customThread,&CustomThread::customThreadSlot); customThread.start(); return a.exec(); }
輸出結(jié)果:
main thread ID: 0x6508
current thread ID: 0x6544thread run finish!
current thread ID(in slot function): 0x6508
4. 總結(jié)
moveToThread
此方式,要求把需要進(jìn)行的工作全部封裝在一個類中,將每一個任務(wù)定義為一個槽函數(shù),并與之對應(yīng)的信號進(jìn)行關(guān)聯(lián),最后調(diào)用moveToThread
將此類交QThread
對象。QThread
調(diào)用start()
進(jìn)行啟動,之后每個任務(wù)由相應(yīng)的信號進(jìn)行觸發(fā)然后執(zhí)行。
QThread
此方式是要求基于QThread
進(jìn)行派生,對派生類進(jìn)行run()
函數(shù)的override
。之后調(diào)用start()
后,就會運(yùn)行run()
函數(shù)。但是在派生類中定義的槽函數(shù),不會由派生類自身所執(zhí)行,而是由該線程的擁有者執(zhí)行。
QThread
只有run
函數(shù)是在新線程里執(zhí)行,其他所有函數(shù)都在QThread
生成的線程里執(zhí)行
官方是比較推薦使用moveToThread
的方式,不過也看各自的使用場景?。?!比如高頻執(zhí)行某個任務(wù)最好還是使用重寫QThread::run()
的方式。
到此這篇關(guān)于QT線程QThread的使用介紹的文章就介紹到這了,更多相關(guān)QT線程QThread內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于OpenCV和C++ 實現(xiàn)圖片旋轉(zhuǎn)
這篇文章主要介紹了基于OpenCV和C++ 實現(xiàn)圖片旋轉(zhuǎn),幫助大家更好的利用c++處理圖片,感興趣的朋友可以了解下2020-12-12