【QT多线程二】继承QObject,movetothread函数来实现多线程

第一种方法,QThread自身并不生存在它run函数所在的线程,而是生存在旧的线程中,线程无法接收信号,只能发送信号。和第一种方法比较,第二种方法使用movetothread更加灵活。

  1. 创建一个 QThread 和 MyThread (继承自 QObject) 类对象;
  2. 使用 moveToThread 函数移动到 thread 中运行;
  3. 通过 thread 类 start 信号和 worker 槽函数绑定;
  4. 调用 thread 类 start 信号 ,运行。

在qt中新建widget项目,新建MyThread类。
mythread.h 新建mythread继承QObject

#ifndef WORKOBJECT_H
#define WORKOBJECT_H


#include 
#include 
#include 
class MyThread : public QObject
{

  Q_OBJECT
public:
     explicit MyThread(QObject *parent = nullptr);

signals:

public slots:
    void doWork();
};

#endif // WORKOBJECT_H

mythread.cpp

#include "mythread.h"
#include 
#include 

MyThread::MyThread(QObject *parent) : QObject(parent)
{

}
void MyThread::doWork()
{

    while (1)
    {
        qDebug()<<tr("mythread QThread::currentThreadId()==")<<QThread::currentThreadId();
        qDebug()<<"66";
        QThread::sleep(1);
    }
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#pragma execution_character_set("utf-8")
#include 
#include 
#include 
#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
    void createView();

    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;
    QThread* thread = new QThread();
    MyThread* mythread = new MyThread();
    Q_PROPERTY(Ui::Widget * ui READ getUi WRITE setUi NOTIFY uiChanged)
};

#endif // WIDGET_H


widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include
#include

Widget::Widget(QWidget *parent)
    : QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

//    QThread* thread = new QThread();
//    MyThread* mythread = new MyThread();

//    QObject::connect(thread, SIGNAL(started()), mythread, SLOT(doWork()));
//    QObject::connect(thread, SIGNAL(finished()), mythread, SLOT(deleteLater()));
    //createView();
    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()));
}

void Widget::createView()
{

}

Ui::Widget *Widget::getUi() const
{
    return ui;
}

void Widget::setUi(Ui::Widget *newUi)
{
    if (ui == newUi)
        return;
    ui = newUi;
    emit uiChanged();
}

void Widget::openThreadBtnSlot()
{

    qDebug() << "thread id:" << QThread::currentThreadId();
    mythread->moveToThread(thread);

    QObject::connect(thread, SIGNAL(started()), mythread, SLOT(doWork()));


    thread->start();

}

void Widget::closeThreadBtnSlot()
{
    thread->quit();
    thread->wait();
    //mythread->deleteLater();
}

void Widget::finishedThreadBtnSlot()
{
    qDebug()<<tr("完成信号finished触发");
}

Widget::~Widget()
{
}

main.cpp

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

两种多线程方式对比:

  • movetoThread是把所需做的工作全部封装到一个类中,每个任务定义为一个槽函数并分别设置信号与他们相连。最后这个类调用movetoThread方法交给一个QThread对象,再调用start函数执行线程。其优点是可以在类中定义多个需要做的工作。
  • 子类化QThread方法,重写run函数,只有run函数里的东西才会被子线程执行,其他在自定义类中定义的函数是在主线程执行的。

你可能感兴趣的:(qt,ui,开发语言)