QT線程QThread的使用介紹
1. 概述
QThread 有兩種使用方式
QObject::moveToThread()
- 派生
QThread
的子類類
2. moveThread 示例
步驟概述:
- 定義一個
QObject
派生類,在派生類中定義一個槽函數,此函數是用于執(zhí)行具體的工作 - 在要使用線程的類中,新建
QThread
和QObject
派生類對象,并使用moveThread()
將派生類的處理交由QThread
- 將觸發(fā)線程工作的信號與派生類的槽函數進行連接
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
的特性機制如信號槽,因為moc
不會在cpp
文件中處理這些機制??梢愿?,但比較麻煩,建議將類定義在頭文件中即可。
3. QThread 示例
方法概述:
- 定義一個
QThread
的派生類,并重載run()
函數,在run()
函數中寫入具體的線程代碼 - 通過
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(); }
輸出結果:
main thread ID: 0x6508
current thread ID: 0x6544thread run finish!
current thread ID(in slot function): 0x6508
4. 總結
moveToThread
此方式,要求把需要進行的工作全部封裝在一個類中,將每一個任務定義為一個槽函數,并與之對應的信號進行關聯(lián),最后調用moveToThread
將此類交QThread
對象。QThread
調用start()
進行啟動,之后每個任務由相應的信號進行觸發(fā)然后執(zhí)行。
QThread
此方式是要求基于QThread
進行派生,對派生類進行run()
函數的override
。之后調用start()
后,就會運行run()
函數。但是在派生類中定義的槽函數,不會由派生類自身所執(zhí)行,而是由該線程的擁有者執(zhí)行。
QThread
只有run
函數是在新線程里執(zhí)行,其他所有函數都在QThread
生成的線程里執(zhí)行
官方是比較推薦使用moveToThread
的方式,不過也看各自的使用場景!??!比如高頻執(zhí)行某個任務最好還是使用重寫QThread::run()
的方式。
到此這篇關于QT線程QThread的使用介紹的文章就介紹到這了,更多相關QT線程QThread內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!