QT線程QThread的使用介紹
1. 概述
QThread 有兩種使用方式
QObject::moveToThread()- 派生
QThread的子類類
2. moveThread 示例
步驟概述:
- 定義一個
QObject派生類,在派生類中定義一個槽函數(shù),此函數(shù)是用于執(zhí)行具體的工作 - 在要使用線程的類中,新建
QThread和QObject派生類對象,并使用moveThread()將派生類的處理交由QThread - 將觸發(fā)線程工作的信號與派生類的槽函數(shù)進行連接
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_HPPThreadController.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_Hmain.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()函數(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_Hmain.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
此方式,要求把需要進行的工作全部封裝在一個類中,將每一個任務(wù)定義為一個槽函數(shù),并與之對應(yīng)的信號進行關(guān)聯(lián),最后調(diào)用moveToThread將此類交QThread對象。QThread調(diào)用start()進行啟動,之后每個任務(wù)由相應(yīng)的信號進行觸發(fā)然后執(zhí)行。
QThread
此方式是要求基于QThread進行派生,對派生類進行run()函數(shù)的override。之后調(diào)用start()后,就會運行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)文章
C語言動態(tài)內(nèi)存管理的實現(xiàn)示例
動態(tài)內(nèi)存管理是一種允許程序在運行時根據(jù)需要動態(tài)申請和回收內(nèi)存的策略,它提供了四種重要的函數(shù),本文就來介紹一下,感興趣的可以了解一下2024-11-11
C++實現(xiàn)LeetCode數(shù)組練習(xí)題
這篇文章主要介紹了C++實現(xiàn)LeetCode的幾道數(shù)組練習(xí)題,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
QT連接Mysql數(shù)據(jù)庫的實現(xiàn)步驟
本文主要介紹了QT連接Mysql數(shù)據(jù)庫的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Linux中使用VS Code編譯調(diào)試C++項目詳解
最近因為項目的需求,需要在Linux下開發(fā)C++相關(guān)項目,經(jīng)過一番摸索最終實現(xiàn)了,下面這篇文章就給大家簡單總結(jié)了一下如何通過VS Code進行編譯調(diào)試的一些注意事項。有需要的朋友們可以參考借鑒,下面來跟著小編一起看看吧。2016-12-12

