網(wǎng)站首頁 編程語言 正文
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: 0x6544?thread 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
的方式,不過也看各自的使用場景!!!比如高頻執(zhí)行某個(gè)任務(wù)最好還是使用重寫QThread::run()
的方式。
原文鏈接:https://blog.csdn.net/weixin_41111116/article/details/126372972
相關(guān)推薦
- 2022-11-13 MacOS下C++使用WebRTC注意事項(xiàng)及問題解決_C 語言
- 2022-03-20 C++中vector容器的注意事項(xiàng)總結(jié)_C 語言
- 2022-06-12 C語言函數(shù)指針數(shù)組實(shí)現(xiàn)計(jì)算器功能_C 語言
- 2022-09-03 Redis實(shí)現(xiàn)Session共享與單點(diǎn)登錄_Redis
- 2022-03-14 IDEA 上傳文件 getRealpath("/upload)獲取不到文件上傳路徑問題
- 2022-08-15 Springboot整合Elasticsearch及相關(guān)API
- 2022-09-26 go使用makefile腳本編譯應(yīng)用的方法小結(jié)_Golang
- 2022-10-16 Pytorch?linear?多維輸入的參數(shù)問題_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支