Qt5.9繼承QObject創(chuàng)建多線程實(shí)例
本博客主要總結(jié)Qt中創(chuàng)建多線程的另一種方法,不是通過(guò)繼承類(lèi)QThread,而是通過(guò)繼承對(duì)象QObject,來(lái)實(shí)現(xiàn)多線程。(可以直接跳過(guò)下面內(nèi)容,看1.1內(nèi)容)
利用繼承QObject方法創(chuàng)建多線程,主要的步驟有一下幾點(diǎn):(注意:退出線程循環(huán)后,還要調(diào)用QThread::quit()函數(shù),該線程才會(huì)觸發(fā)QThread::finished()信號(hào))
a1:首先創(chuàng)建一個(gè)類(lèi)MyThread,基類(lèi)為QObject。
a2:在類(lèi)MyThread中創(chuàng)建一個(gè)槽函數(shù),用于運(yùn)行多線程里面的代碼。所有耗時(shí)代碼,全部在這個(gè)槽函數(shù)里面運(yùn)行。
a3:實(shí)例一個(gè)QThread線程對(duì)象(容器),將類(lèi)MyThread的實(shí)例對(duì)象轉(zhuǎn)到該容器中,用函數(shù)void QObject::moveToThread(QThread *thread);
myObjectThread->moveToThread(firstThread);
a4:用一個(gè)信號(hào)觸發(fā)該多線程槽函數(shù),比如用QThread::started()信號(hào)。
connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot()));
a5:用信號(hào)QThread::finished綁定槽函數(shù)QThread::deleteLatater(),在線程退出時(shí),銷(xiāo)毀該線程和相關(guān)資源。
connect(firstThread,SIGNAL(finished()),firstThread,SLOT(deleteLater()));
a6:所有線程初始化完成后,啟動(dòng)函數(shù)QThread::start()開(kāi)啟多線程,然后自動(dòng)觸發(fā)多線程啟動(dòng)信號(hào)QThread::started()。
具體的教程如下圖所示:
1.1新建一個(gè)widget工程,不要勾選ui界面。然后分別在mythread.h,mythread.cpp,widget.h,widget.cpp,main.cpp分別添加如下代碼。
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
class MyThread : public QObject
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
void closeThread();
signals:
public slots:
void startThreadSlot();
private:
volatile bool isStop;
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <QDebug>
#include <QThread>
MyThread::MyThread(QObject *parent) : QObject(parent)
{
isStop = false;
}
void MyThread::closeThread()
{
isStop = true;
}
void MyThread::startThreadSlot()
{
while (1)
{
if(isStop)
return;
qDebug()<<"MyThread::startThreadSlot QThread::currentThreadId()=="<<QThread::currentThreadId();
QThread::sleep(1);
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QThread>
#include "mythread.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void createView();
private slots:
void openThreadSlot();
void closeThreadSlot();
void finishedThreadSlot();
private:
QVBoxLayout *mainLayout;
QThread *firstThread;
MyThread *myObjectThread;
};
#endif // WIDGET_H
widget.cpp
#include <QDebug>
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
createView();
}
Widget::~Widget()
{
}
void Widget::createView()
{
/*UI界面*/
mainLayout = new QVBoxLayout(this);
QPushButton *openThreadBtn = new QPushButton(tr("打開(kāi)線程"));
QPushButton *closeThreadBtn = new QPushButton(tr("關(guān)閉線程"));
mainLayout->addWidget(openThreadBtn);
mainLayout->addWidget(closeThreadBtn);
mainLayout->addStretch();
connect(openThreadBtn,SIGNAL(clicked(bool)),this,SLOT(openThreadSlot()));
connect(closeThreadBtn,SIGNAL(clicked(bool)),this,SLOT(closeThreadSlot()));
}
void Widget::openThreadSlot()
{
/*開(kāi)啟一條多線程*/
qDebug()<<tr("開(kāi)啟線程");
firstThread = new QThread; //線程容器
myObjectThread = new MyThread;
myObjectThread->moveToThread(firstThread); //將創(chuàng)建的對(duì)象移到線程容器中
connect(firstThread,SIGNAL(finished()),myObjectThread,SLOT(deleteLater())); //終止線程時(shí)要調(diào)用deleteLater槽函數(shù)
connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot())); //開(kāi)啟線程槽函數(shù)
connect(firstThread,SIGNAL(finished()),this,SLOT(finishedThreadSlot()));
firstThread->start(); //開(kāi)啟多線程槽函數(shù)
qDebug()<<"mainWidget QThread::currentThreadId()=="<<QThread::currentThreadId();
}
void Widget::closeThreadSlot()
{
qDebug()<<tr("關(guān)閉線程");
if(firstThread->isRunning())
{
myObjectThread->closeThread(); //關(guān)閉線程槽函數(shù)
firstThread->quit(); //退出事件循環(huán)
firstThread->wait(); //釋放線程槽函數(shù)資源
}
}
void Widget::finishedThreadSlot()
{
qDebug()<<tr("多線程觸發(fā)了finished信號(hào)123");
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(960,640);
w.show();
return a.exec();
}
1.2程序構(gòu)建和運(yùn)行后,結(jié)果如下圖所示:


開(kāi)啟多線程

關(guān)閉多線程
參考內(nèi)容:
https://blog.csdn.net/qq_41672557/article/details/80324251(重點(diǎn)參考步驟)
https://blog.csdn.net/czyt1988/article/details/71194457(重點(diǎn)參考代碼)
到此這篇關(guān)于Qt5.9繼承QObject創(chuàng)建多線程實(shí)例的文章就介紹到這了,更多相關(guān)Qt5.9 創(chuàng)建多線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中的opeartor?new和placement?new使用步驟
這篇文章主要介紹了C++中的opeartor?new和placement?new詳解,在很多情況下,placement?new的使用方法和其他普通的new有所不同。這里提供了它的使用步驟,需要的朋友可以參考下2022-10-10
基于Qt Qml實(shí)現(xiàn)時(shí)間軸組件
時(shí)間軸組件是現(xiàn)代用戶(hù)界面中常見(jiàn)的元素,用于按時(shí)間順序展示事件,本文主要為大家詳細(xì)介紹了如何使用Qml實(shí)現(xiàn)一個(gè)簡(jiǎn)單的時(shí)間軸組件,需要的可以參考下2025-01-01
C++簡(jiǎn)單實(shí)現(xiàn)shared_ptr的代碼
智能指針用于資源管理,為了保證資源的操作得到順利的執(zhí)行防止資源泄露,因此大多數(shù)實(shí)現(xiàn)都以noexcept在參數(shù)列表后聲明為不拋出異常,這篇文章主要介紹了C++簡(jiǎn)單實(shí)現(xiàn)shared_ptr的代碼,需要的朋友可以參考下2022-09-09
新手向超詳細(xì)的C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)順序表
本文主要介紹了C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)順序表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
C/C++使用socket實(shí)現(xiàn)判斷ip是否能連通
這篇文章主要為大家詳細(xì)介紹了C/C++如何使用socket實(shí)現(xiàn)判斷ip是否能連通,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07
C++實(shí)現(xiàn)LeetCode(159.最多有兩個(gè)不同字符的最長(zhǎng)子串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(159.最多有兩個(gè)不同字符的最長(zhǎng)子串),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

