Qt5.9继承QObject创建多线程实例

时间:2021-07-18 15:58:53

本博客主要总结Qt中创建多线程的另一种方法,不是通过继承类QThread,而是通过继承对象QObject,来实现多线程。(可以直接跳过下面内容,看1.1内容

利用继承QObject方法创建多线程,主要的步骤有一下几点:(注意:退出线程循环后,还要调用QThread::quit()函数,该线程才会触发QThread::finished()信号)

a1:首先创建一个类MyThread,基类为QObject。

a2:在类MyThread中创建一个槽函数,用于运行多线程里面的代码。所有耗时代码,全部在这个槽函数里面运行。

a3:实例一个QThread线程对象(容器),将类MyThread的实例对象转到该容器中,用函数void QObject::moveToThread(QThread *thread);

myObjectThread->moveToThread(firstThread);

a4:用一个信号触发该多线程槽函数,比如用QThread::started()信号。

connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot()));

a5:用信号QThread::finished绑定槽函数QThread::deleteLatater(),在线程退出时,销毁该线程和相关资源。

connect(firstThread,SIGNAL(finished()),firstThread,SLOT(deleteLater()));

a6:所有线程初始化完成后,启动函数QThread::start()开启多线程,然后自动触发多线程启动信号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("关闭线程"));
    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);                                      //将创建的对象移到线程容器中
    connect(firstThread,SIGNAL(finished()),myObjectThread,SLOT(deleteLater()));        //终止线程时要调用deleteLater槽函数
    connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot()));  //开启线程槽函数
    connect(firstThread,SIGNAL(finished()),this,SLOT(finishedThreadSlot()));
    firstThread->start();                                                           //开启多线程槽函数
    qDebug()<<"mainWidget QThread::currentThreadId()=="<<QThread::currentThreadId();
}
 
void Widget::closeThreadSlot()
{        
    qDebug()<<tr("关闭线程");
    if(firstThread->isRunning())
    {
        myObjectThread->closeThread();  //关闭线程槽函数
        firstThread->quit();            //退出事件循环
        firstThread->wait();            //释放线程槽函数资源
    }
}
 
void Widget::finishedThreadSlot()
{
    qDebug()<<tr("多线程触发了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程序构建和运行后,结果如下图所示:

Qt5.9继承QObject创建多线程实例

Qt5.9继承QObject创建多线程实例

                                                                                                       开启多线程

Qt5.9继承QObject创建多线程实例

                                                                                                                关闭多线程

参考内容:

https://blog.csdn.net/qq_41672557/article/details/80324251(重点参考步骤)

https://blog.csdn.net/czyt1988/article/details/71194457(重点参考代码)

到此这篇关于Qt5.9继承QObject创建多线程实例的文章就介绍到这了,更多相关Qt5.9 创建多线程内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/naibozhuan3744/article/details/81201502