Qt5.9繼承QObject創(chuàng)建多線程實例
本博客主要總結(jié)Qt中創(chuàng)建多線程的另一種方法,不是通過繼承類QThread,而是通過繼承對象QObject,來實現(xiàn)多線程。(可以直接跳過下面內(nèi)容,看1.1內(nèi)容)
利用繼承QObject方法創(chuàng)建多線程,主要的步驟有一下幾點:(注意:退出線程循環(huán)后,還要調(diào)用QThread::quit()函數(shù),該線程才會觸發(fā)QThread::finished()信號)
a1:首先創(chuàng)建一個類MyThread,基類為QObject。
a2:在類MyThread中創(chuàng)建一個槽函數(shù),用于運行多線程里面的代碼。所有耗時代碼,全部在這個槽函數(shù)里面運行。
a3:實例一個QThread線程對象(容器),將類MyThread的實例對象轉(zhuǎn)到該容器中,用函數(shù)void QObject::moveToThread(QThread *thread);
myObjectThread->moveToThread(firstThread);
a4:用一個信號觸發(fā)該多線程槽函數(shù),比如用QThread::started()信號。
connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot()));
a5:用信號QThread::finished綁定槽函數(shù)QThread::deleteLatater(),在線程退出時,銷毀該線程和相關(guān)資源。
connect(firstThread,SIGNAL(finished()),firstThread,SLOT(deleteLater()));
a6:所有線程初始化完成后,啟動函數(shù)QThread::start()開啟多線程,然后自動觸發(fā)多線程啟動信號QThread::started()。
具體的教程如下圖所示:
1.1新建一個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("打開線程")); 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() { /*開啟一條多線程*/ qDebug()<<tr("開啟線程"); firstThread = new QThread; //線程容器 myObjectThread = new MyThread; myObjectThread->moveToThread(firstThread); //將創(chuàng)建的對象移到線程容器中 connect(firstThread,SIGNAL(finished()),myObjectThread,SLOT(deleteLater())); //終止線程時要調(diào)用deleteLater槽函數(shù) connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot())); //開啟線程槽函數(shù) connect(firstThread,SIGNAL(finished()),this,SLOT(finishedThreadSlot())); firstThread->start(); //開啟多線程槽函數(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信號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)建和運行后,結(jié)果如下圖所示:
開啟多線程
關(guān)閉多線程
參考內(nèi)容:
https://blog.csdn.net/qq_41672557/article/details/80324251(重點參考步驟)
https://blog.csdn.net/czyt1988/article/details/71194457(重點參考代碼)
到此這篇關(guān)于Qt5.9繼承QObject創(chuàng)建多線程實例的文章就介紹到這了,更多相關(guān)Qt5.9 創(chuàng)建多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ WideCharToMultiByte()函數(shù)案例詳解
這篇文章主要介紹了C++ WideCharToMultiByte()函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08C++調(diào)用Python基礎(chǔ)功能實例詳解
c++調(diào)用Python首先安裝Python,本文以win7為例,給大家詳細介紹C++調(diào)用Python基礎(chǔ)功能,需要的朋友參考下吧2017-04-04C++?RBTree紅黑樹的性質(zhì)與實現(xiàn)
紅黑樹是一種二叉搜索樹,但在每個結(jié)點上增加一個存儲位表示結(jié)點的顏色,可以是Red或Black;通過對任何一條從根到葉子的路徑上各個結(jié)點著色方式的限制,紅黑樹確保沒有一條路徑會比其他路徑長出倆倍,因而是平衡的2023-03-03C語言實現(xiàn)的統(tǒng)計素數(shù)并求和代碼分享
這篇文章主要介紹了C語言實現(xiàn)的統(tǒng)計素數(shù)并求和代碼分享,來自PAT平臺(浙江大學(xué)計算機程序設(shè)計能力考試系統(tǒng))的一個題目,需要的朋友可以參考下2014-08-08