本篇文章给大家分享的是有关Qt5.9如何继承QObject创建多线程,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
利用继承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创建多线程,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。