




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第QT線程QThread的使用介紹目錄1.概述2.moveThread示例3.QThread示例4.總結(jié)
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代碼如下:
#ifndefTHREADWORKER_HPP
#defineTHREADWORKER_HPP
#includeQObject
#includeQString
#includeQThread
#includeQDebug
classThreadWorker:publicQObject
Q_OBJECT
public:
ThreadWorker(){}
publicslots:
voidwork(QStringp1)
qDebug()"currentthreadID:"QThread::currentThreadId();
qDebug()p1;
QThread::sleep(10);
qDebug()"threadrunfinish!";
#endif//THREADWORKER_HPP
ThreadController.hpp代碼如下:
#ifndefTHREADCONTROLLER_H
#defineTHREADCONTROLLER_H
#include"ThreadWorker.hpp"
classThreadController:publicQObject
Q_OBJECT
QThreadworkerThread;
public:
ThreadController():QObject()
ThreadWorker*threadWork=newThreadWorker();
//將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()"currentthreadID:"QThread::currentThreadId()'\n';
emittouchWork("working");
~ThreadController()
workerThread.quit();
workerThread.wait();
signals:
//發(fā)出信號(hào)觸發(fā)線程
voidtouchWork(QStringp1);
#endif//THREADCONTROLLER_H
main.cpp代碼如下:
#includeQCoreApplication
#include"ThreadController.hpp"
intmain(intargc,char*argv[])
QCoreApplicationa(argc,argv);
ThreadControllertc=ThreadController();
returna.exec();
}
注意:
不能再cpp文件中使用QT的特性機(jī)制如信號(hào)槽,因?yàn)閙oc不會(huì)在cpp文件中處理這些機(jī)制??梢愿?,但比較麻煩,建議將類定義在頭文件中即可。
3.QThread示例
方法概述:
定義一個(gè)QThread的派生類,并重載run()函數(shù),在run()函數(shù)中寫入具體的線程代碼通過(guò)start()啟動(dòng)線程
CustomThread.hpp代碼如下
#ifndefCUSTOMTHREAD_H
#defineCUSTOMTHREAD_H
#includeQThread
#includeQDebug
classCustomThread:publicQThread
Q_OBJECT
public:
CustomThread(){}
signals:
voidcustomThreadSignal();
publicslots:
voidcustomThreadSlot()
qDebug()"currentthreadID(inslotfunction):"QThread::currentThreadId()'\n';
protected:
voidrun()override
qDebug()"currentthreadID:"QThread::currentThreadId()'\n';
QThread::sleep(10);
qDebug()"threadrunfinish!";
emitcustomThreadSignal();
#endif//CUSTOMTHREAD_H
main.cpp代碼如下
#includeQCoreApplication
#include"CustomThread.hpp"
intmain(intargc,char*argv[])
QCoreApplicationa(argc,argv);
qDebug()"mainthreadID:"QThread::currentThreadId();
CustomThreadcustomThread;
QObject::connect(customThread,CustomThread::customThreadSignal,customThread,CustomThread::customThreadSlot);
customThread.start();
returna.exec();
}
輸出結(jié)果:
mainthreadID:0x6508
currentthreadID:0x6544
threadrunfinish!
currentthreadID(inslotfunction):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ù)。但是
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 西醫(yī)外科學(xué)練習(xí)題
- 保潔員初級(jí)模擬試題
- 網(wǎng)絡(luò)安全技術(shù)實(shí)踐測(cè)試題
- 浙江國(guó)企招聘2025杭州市臨安區(qū)城市發(fā)展投資集團(tuán)有限公司下屬子公司招聘8人筆試參考題庫(kù)附帶答案詳解
- 2025福建武夷交通運(yùn)輸股份有限公司招聘10人筆試參考題庫(kù)附帶答案詳解
- 2025年黃山旅游發(fā)展股份有限公司春季招聘75人筆試參考題庫(kù)附帶答案詳解
- 青馬試題填空題及答案
- 鏟車路障考試題及答案
- 紡織品設(shè)計(jì)師證書試題及答案匯集
- 固廢處理項(xiàng)目前景評(píng)估與策略分析
- (市質(zhì)檢)莆田市2025屆高中畢業(yè)班第四次教學(xué)質(zhì)量檢測(cè)試卷語(yǔ)文試卷(含答案解析)
- 瓷磚空鼓裝修合同協(xié)議
- 2025年四川筠連縣國(guó)有資本投資運(yùn)營(yíng)有限公司招聘筆試參考題庫(kù)含答案解析
- 2024年貴州遵義公開招聘社區(qū)工作者考試試題答案解析
- 中職生職業(yè)生涯課件
- 煙臺(tái)2025年煙臺(tái)市蓬萊區(qū)“蓬選”考選90人筆試歷年參考題庫(kù)附帶答案詳解
- 2025年全國(guó)低壓電工證(復(fù)審)考試筆試試題(300題)含答案
- 2025年浙江省生態(tài)環(huán)境廳所屬事業(yè)單位招聘考試備考題庫(kù)
- 入團(tuán)考試測(cè)試題及答案
- 【語(yǔ)文試卷+答案 】上海市崇明區(qū)2025屆高三第二學(xué)期第二次模擬考試(崇明二模)
- 《中國(guó)古典文學(xué)中的詠花詩(shī)與審美文化》論文
評(píng)論
0/150
提交評(píng)論