您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Qt5.9如何繼承QObject創(chuàng)建多線程,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
利用繼承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)閉多線程
以上就是Qt5.9如何繼承QObject創(chuàng)建多線程,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。