QT線程QThread的使用介紹
1. 概述
QThread 有兩種使用方式
QObject::moveToThread()
- 派生
QThread
的子類類
2. moveThread 示例
步驟概述:
- 定義一個(gè)
QObject
派生類,在派生類中定義一個(gè)槽函數(shù),此函數(shù)是用于執(zhí)行具體的工作 - 在要使用線程的類中,新建
QThread
和QObject
派生類對(duì)象,并使用moveThread()
將派生類的處理交由QThread
- 將觸發(fā)線程工作的信號(hào)與派生類的槽函數(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(); //啟動(dòng)線程 qDebug()<<"current thread ID:"<<QThread::currentThreadId()<<'\n'; emit touchWork("working"); } ~ThreadController() { workerThread.quit(); workerThread.wait(); } signals: // 發(fā)出信號(hào)觸發(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ī)制如信號(hào)槽,因?yàn)?code>moc不會(huì)在cpp
文件中處理這些機(jī)制??梢愿?,但比較麻煩,建議將類定義在頭文件中即可。
3. QThread 示例
方法概述:
- 定義一個(gè)
QThread
的派生類,并重載run()
函數(shù),在run()
函數(shù)中寫入具體的線程代碼 - 通過
start()
啟動(dòng)線程
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)行的工作全部封裝在一個(gè)類中,將每一個(gè)任務(wù)定義為一個(gè)槽函數(shù),并與之對(duì)應(yīng)的信號(hào)進(jìn)行關(guān)聯(lián),最后調(diào)用moveToThread
將此類交QThread
對(duì)象。QThread
調(diào)用start()
進(jìn)行啟動(dòng),之后每個(gè)任務(wù)由相應(yīng)的信號(hào)進(jìn)行觸發(fā)然后執(zhí)行。
QThread
此方式是要求基于QThread
進(jìn)行派生,對(duì)派生類進(jìn)行run()
函數(shù)的override
。之后調(diào)用start()
后,就會(huì)運(yùn)行run()
函數(shù)。但是在派生類中定義的槽函數(shù),不會(huì)由派生類自身所執(zhí)行,而是由該線程的擁有者執(zhí)行。
QThread
只有run
函數(shù)是在新線程里執(zhí)行,其他所有函數(shù)都在QThread
生成的線程里執(zhí)行
官方是比較推薦使用moveToThread
的方式,不過也看各自的使用場(chǎng)景?。。”热绺哳l執(zhí)行某個(gè)任務(wù)最好還是使用重寫QThread::run()
的方式。
到此這篇關(guān)于QT線程QThread的使用介紹的文章就介紹到這了,更多相關(guān)QT線程QThread內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中遞歸的實(shí)際應(yīng)用與經(jīng)典問題
函數(shù)以及函數(shù)的遞歸調(diào)用是學(xué)習(xí)C語言必須要掌握的內(nèi)容,且遞歸作為經(jīng)典的算法思想被廣泛應(yīng)用于程序設(shè)計(jì)中,下面這篇文章主要給大家介紹了關(guān)于C語言中遞歸的實(shí)際應(yīng)用與經(jīng)典問題的相關(guān)資料,需要的朋友可以參考下2021-09-09C語言-I/O流設(shè)計(jì)實(shí)驗(yàn)
編程語言的I/O類庫中常常使用流這個(gè)抽象的概念,它代表任何有能力產(chǎn)生數(shù)據(jù)的數(shù)據(jù)源對(duì)象或時(shí)有能力接收數(shù)據(jù)的接收端對(duì)象,本文為大家介紹C語言中I/O系統(tǒng)基礎(chǔ)知識(shí)2021-07-07基于OpenCV和C++ 實(shí)現(xiàn)圖片旋轉(zhuǎn)
這篇文章主要介紹了基于OpenCV和C++ 實(shí)現(xiàn)圖片旋轉(zhuǎn),幫助大家更好的利用c++處理圖片,感興趣的朋友可以了解下2020-12-12C++ 實(shí)現(xiàn)稀疏矩陣的壓縮存儲(chǔ)的實(shí)例
這篇文章主要介紹了C++ 實(shí)現(xiàn)稀疏矩陣的壓縮存儲(chǔ)的實(shí)例的相關(guān)資料,M*N的矩陣,矩陣中有效值的個(gè)數(shù)遠(yuǎn)小于無效值的個(gè)數(shù),且這些數(shù)據(jù)的分布沒有規(guī)律,需要的朋友可以參考下2017-07-07