Qt5.9實現(xiàn)簡單的多線程實例(類QThread)
Qt開啟多線程,主要用到類QThread。有兩種方法,第一種用一個類繼承QThread,然后重新改寫虛函數(shù)run()。當(dāng)要開啟新線程時,只需要實例該類,然后調(diào)用函數(shù)start(),就可以開啟一條多線程。第二種方法是繼承一個QObject類,然后利用moveToThread()函數(shù)開啟一個線程槽函數(shù),將要花費(fèi)大量時間計算的代碼放入該線程槽函數(shù)中。第二種方法可以參考我寫的另一篇博客:http://www.dbjr.com.cn/article/223796.htm
下面我總結(jié)的主要是第一種方法。(注意:只有在run()函數(shù)里面才是新的線程,所有復(fù)雜邏輯都應(yīng)該在run()函數(shù)里面做。當(dāng)run()函數(shù)運(yùn)行完畢后,該線程生命周期結(jié)束。)
創(chuàng)建多線程步驟如下:
a1新建一個類MyThread,基類為QThread。
a2重寫類MyThread的虛函數(shù)void run();,即新建一個函數(shù)protected void run(),然后對其進(jìn)行定義。
a3在需要用到多線程的地方,實例MyThread,然后調(diào)用函數(shù)MyThread::start()后,則開啟一條線程,自動運(yùn)行函數(shù)run()。
a4當(dāng)停止線程時,調(diào)用MyThread::wait()函數(shù),等待線程結(jié)束,并且回收線程資源。
1.1新建一個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()
{
/*開啟一個線程*/
thread1->start();
qDebug()<<"主線程id:"<<QThread::currentThreadId();
}
void Widget::closeThreadBtnSlot()
{
/*關(guān)閉多線程*/
thread1->closeThread();
thread1->wait();
}
void Widget::finishedThreadBtnSlot()
{
qDebug()<<tr("完成信號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實現(xiàn)簡單的多線程實例(類QThread)的文章就介紹到這了,更多相關(guān)Qt5.9 多線程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中strcpy和strcat的使用和模擬實現(xiàn)
strcpy()?函數(shù)是?C語言中一個非常重要的字符串處理函數(shù),其功能是將一個字符串復(fù)制到另一個字符串中,strcat函數(shù)可以將一個字符串拼接到另一個字符串的末尾,本文給大家介紹了C語言中strcpy和strcat的使用和模擬實現(xiàn),需要的朋友可以參考下2024-03-03
《C++ Primer》隱式類類型轉(zhuǎn)換學(xué)習(xí)整理
在本篇文章里小編給大家整理的是關(guān)于《C++ Primer》隱式類類型轉(zhuǎn)換學(xué)習(xí)筆記內(nèi)容,需要的朋友們參考下。2020-02-02

