Qt5.9實(shí)現(xiàn)簡(jiǎn)單的多線程實(shí)例(類QThread)
Qt開啟多線程,主要用到類QThread。有兩種方法,第一種用一個(gè)類繼承QThread,然后重新改寫虛函數(shù)run()。當(dāng)要開啟新線程時(shí),只需要實(shí)例該類,然后調(diào)用函數(shù)start(),就可以開啟一條多線程。第二種方法是繼承一個(gè)QObject類,然后利用moveToThread()函數(shù)開啟一個(gè)線程槽函數(shù),將要花費(fèi)大量時(shí)間計(jì)算的代碼放入該線程槽函數(shù)中。第二種方法可以參考我寫的另一篇博客:http://www.dhdzp.com/article/223796.htm
下面我總結(jié)的主要是第一種方法。(注意:只有在run()函數(shù)里面才是新的線程,所有復(fù)雜邏輯都應(yīng)該在run()函數(shù)里面做。當(dāng)run()函數(shù)運(yùn)行完畢后,該線程生命周期結(jié)束。)
創(chuàng)建多線程步驟如下:
a1新建一個(gè)類MyThread,基類為QThread。
a2重寫類MyThread的虛函數(shù)void run();,即新建一個(gè)函數(shù)protected void run(),然后對(duì)其進(jìn)行定義。
a3在需要用到多線程的地方,實(shí)例MyThread,然后調(diào)用函數(shù)MyThread::start()后,則開啟一條線程,自動(dòng)運(yùn)行函數(shù)run()。
a4當(dāng)停止線程時(shí),調(diào)用MyThread::wait()函數(shù),等待線程結(jié)束,并且回收線程資源。
1.1新建一個(gè)widget工程,不要勾選ui界面。然后分別在mythread.h,mythread.cpp,widget.h,widget.cpp,main.cpp分別添加如下代碼。
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
class MyThread : public QThread
{
public:
MyThread();
void closeThread();
protected:
virtual void run();
private:
volatile bool isStop; //isStop是易失性變量,需要用volatile進(jìn)行申明
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <QDebug>
#include <QMutex>
MyThread::MyThread()
{
isStop = false;
}
void MyThread::closeThread()
{
isStop = true;
}
void MyThread::run()
{
while (1)
{
if(isStop)
return;
qDebug()<<tr("mythread QThread::currentThreadId()==")<<QThread::currentThreadId();
sleep(1);
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <mythread.h>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMutex>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void createView();
private slots:
void openThreadBtnSlot();
void closeThreadBtnSlot();
void finishedThreadBtnSlot();
// void testBtnSlot();
private:
QVBoxLayout *mainLayout;
MyThread *thread1;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QDebug>
#include <windows.h>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
createView();
}
void Widget::createView()
{
/*添加界面*/
QPushButton *openThreadBtn = new QPushButton(tr("打開線程"));
QPushButton *closeThreadBtn = new QPushButton(tr("關(guān)閉線程"));
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(openThreadBtn);
mainLayout->addWidget(closeThreadBtn);
mainLayout->addStretch();
connect(openThreadBtn,SIGNAL(clicked(bool)),this,SLOT(openThreadBtnSlot()));
connect(closeThreadBtn,SIGNAL(clicked(bool)),this,SLOT(closeThreadBtnSlot()));
/*線程初始化*/
thread1 = new MyThread;
connect(thread1,SIGNAL(finished()),this,SLOT(finishedThreadBtnSlot()));
}
void Widget::openThreadBtnSlot()
{
/*開啟一個(gè)線程*/
thread1->start();
qDebug()<<"主線程id:"<<QThread::currentThreadId();
}
void Widget::closeThreadBtnSlot()
{
/*關(guān)閉多線程*/
thread1->closeThread();
thread1->wait();
}
void Widget::finishedThreadBtnSlot()
{
qDebug()<<tr("完成信號(hào)finished觸發(fā)");
}
Widget::~Widget()
{
}
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é)果如下圖所示:



參考內(nèi)容:
https://blog.csdn.net/czyt1988/article/details/64441443(正確終止線程經(jīng)典教程)
https://blog.csdn.net/MyCodingLine/article/details/48597935
https://blog.csdn.net/xipiaoyouzi/article/details/8450704
https://blog.csdn.net/qq769651718/article/details/79357933(兩種創(chuàng)建多線程方式)
https://blog.csdn.net/czyt1988/article/details/71194457
到此這篇關(guān)于Qt5.9實(shí)現(xiàn)簡(jiǎn)單的多線程實(shí)例(類QThread)的文章就介紹到這了,更多相關(guān)Qt5.9 多線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言使用廣度優(yōu)先搜索算法解決迷宮問(wèn)題(隊(duì)列)
這篇文章主要介紹了C語(yǔ)言使用廣度優(yōu)先搜索算法解決迷宮問(wèn)題,結(jié)合迷宮問(wèn)題分析了C語(yǔ)言隊(duì)列廣度優(yōu)先搜索算法的相關(guān)使用技巧,需要的朋友可以參考下2017-09-09
C語(yǔ)言之函數(shù)返回值與參數(shù)傳遞案例教程
這篇文章主要介紹了C語(yǔ)言之函數(shù)返回值與參數(shù)傳遞案例教程,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)LeetCode(61.旋轉(zhuǎn)鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(61.旋轉(zhuǎn)鏈表),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言實(shí)現(xiàn)掃雷小游戲(擴(kuò)展版可選擇游戲難度)
游戲目標(biāo)是找出所有沒(méi)有地雷的方格,完成游戲;要是按了有地雷的方格,游戲失?。煌婕铱蓸?biāo)記雷的位置,游戲以完成時(shí)間來(lái)評(píng)高低,并且用戶可以選擇游戲難度2019-10-10
基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的掃雷游戲
這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C語(yǔ)言文件操作實(shí)現(xiàn)數(shù)據(jù)持久化(幫你快速了解文件操作函數(shù))
持久數(shù)據(jù)其實(shí)就是將數(shù)據(jù)保存到數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言文件操作實(shí)現(xiàn)數(shù)據(jù)持久化(幫你快速了解文件操作函數(shù))的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
貪心算法的C語(yǔ)言實(shí)現(xiàn)與運(yùn)用詳解
這篇文章主要介紹了貪心算法的C語(yǔ)言實(shí)現(xiàn)與運(yùn)用詳解,運(yùn)用么,就是文中所附的ACM練習(xí)題,哈哈:D需要的朋友可以參考下2015-08-08

