Qt有兩種多線程的方法,其中一種是繼承QThread的run函數(shù),另外一種是把一個(gè)繼承于QObject的類轉(zhuǎn)移到一個(gè)Thread里。
在這里先介紹一下qt多線程的第一種實(shí)現(xiàn)方法,繼承qthread并重寫run函數(shù)。
注意:QThread只有run函數(shù)是在新線程里的,其他所有函數(shù)都在QThread生成的線程里。如果QThread是在ui所在的線程里生成,那么QThread的其他非run函數(shù)都是和ui線程一樣的,所以,QThread的繼承類的其他函數(shù)盡量別要有太耗時(shí)的操作,要確保所有耗時(shí)的操作都在run函數(shù)里。
下面給出通過重寫run函數(shù)實(shí)現(xiàn)多線程的小例子。
- 首先使用qt新建一個(gè)widget項(xiàng)目,然后在項(xiàng)目里添加MyThread類。
- MyThread類繼承QThread類,并在cpp文件里重寫run函數(shù)。
- 在widget中使用start函數(shù)開啟線程,啟動(dòng)run函數(shù)。
- run函數(shù)結(jié)束之前會(huì)發(fā)送finished信號(hào),可自定義槽函數(shù)。
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();
qDebug()<<"66";
sleep(1);
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#pragma execution_character_set("utf-8")
#include <QWidget>
#include <mythread.h>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMutex>
#include <ui_widget.h>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
Ui::Widget *ui;
Ui::Widget *getUi() const;
void setUi(Ui::Widget *newUi);
signals:
void uiChanged();
private slots:
void openThreadBtnSlot();
void closeThreadBtnSlot();
void finishedThreadBtnSlot();
// void testBtnSlot();
private:
QVBoxLayout *mainLayout;
MyThread *thread1;
Q_PROPERTY(Ui::Widget * ui READ getUi WRITE setUi NOTIFY uiChanged)
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <windows.h>
#include<ui_widget.h>
Widget::Widget(QWidget *parent)
: QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->openThreadBtn,SIGNAL(clicked(bool)),this,SLOT(openThreadBtnSlot()));
connect(ui->closeThreadBtn,SIGNAL(clicked(bool)),this,SLOT(closeThreadBtnSlot()));
//connect(thread1,SIGNAL(finished()),this,SLOT(finishedThreadBtnSlot()));
}
Ui::Widget *Widget::getUi() const
{
return ui;
}
void Widget::setUi(Ui::Widget *newUi)
{
if (ui == newUi)
return;
ui = newUi;
emit uiChanged();
}
void Widget::openThreadBtnSlot()
{
//開啟一個(gè)線程
thread1 = new MyThread;
thread1->start();
qDebug()<<"thread id:"<<QThread::currentThreadId();
}
void Widget::closeThreadBtnSlot()
{
//關(guān)閉多線程
thread1->closeThread();
connect(thread1,SIGNAL(finished()),this,SLOT(finishedThreadBtnSlot()));
thread1->quit();
thread1->wait();
}
void Widget::finishedThreadBtnSlot()
{
qDebug()<<tr("完成信號(hào)finished觸發(fā)");
}
Widget::~Widget()
{
}
main.cpp文章來源:http://www.zghlxwxcb.cn/news/detail-632412.html
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
注意在widget.ui里面添加兩個(gè)按鈕openThreadBtn和closeThreadBtn控制線程的開啟和結(jié)束。文章來源地址http://www.zghlxwxcb.cn/news/detail-632412.html
到了這里,關(guān)于【QT多線程一】繼承QThread,重寫run函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!