alin的学习之路:Qt与多线程

alin的学习之路:Qt与多线程

如果程序在进行复杂的逻辑处理过程中, 对窗口进行操作, 就会出现无响应的情况。

如何解决这样的问题与高并发的问题?

需要使用多线程。

方式1

特点:简单

  1. 创建一个自定义的类,先选择继承QObject,随后改为继承QThread

  2. 重写父类的run()方法,在run()方法里面写子线程的执行逻辑。注意run函数是protected的作用域

    [virtual protected] void QThread::run()
    
  3. 在主线程中创建子线程的对象

  4. 使用子线程对象调用start()方法,即开始执行子线程的逻辑

    [slot] void QThread::start(Priority priority = InheritPriority)
    不能在类的外部调用run() 方法启动子线程, 在外部调用start()相当于让run()开始运行
    

线程间通信:

子线程类定义一个槽函数,将需要通信的数据放在信号函数的参数中,在子线程的run()方法中触发信号。

注意事项: 在Qt中在子线程中不要操作程序中的窗口类型对象, 不允许, 如果操作了程序就挂了,只有主线程才能操作程序中的窗口对象, 默认的线程就是主线程, 自己创建的就是子线程

demo

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QThread>

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

protected:
    void run();

signals:
    void curNumber(int num);

public slots:
};

#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"

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

}

void MyThread::run()
{
    int num = 0;
    for(int i=0; i<10000000; ++i)
    {
        emit curNumber(num++);
        usleep(1);
    }
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
#include <QThread>

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

    MyThread* mythread = new MyThread;


    connect(ui->pushButton, &QPushButton::clicked, this, [=](){
         mythread->start();
    });

    connect(mythread, &MyThread::curNumber, this, [=](int num){
        ui->label->setNum(num);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

方式2

特点:灵活

  1. 创建一个工作类,定义一个工作函数,这个工作函数就是子线程的执行代码。
  2. 主线程中创建一个工作类的对象
    • 千万不要指定给创建的对象指定父对象
  3. 主线程中创建一个QThread类的对象
  4. 将工作类对象移动到创建的线程对象中,使用QObject类提供的api, moveToThread
    • void QObject::moveToThread(QThread *targetThread);
    • work->moveToThread(sub); // 移动到子线程中工作
  5. 启动子线程,调用线程对象中的start()方法,此时并没有开始执行子线程的代码逻辑
  6. 调用工作对象的工作函数,此时子线程开始执行

线程间通信:

需要使用槽函数

demo

mywork.h

#ifndef MYWORK_H
#define MYWORK_H

#include <QObject>
#include <QThread>

class MyWork : public QObject
{
    Q_OBJECT
public:
    explicit MyWork(QObject *parent = nullptr);

    void doWork();
signals:
    void curNumber(int num);

public slots:
};

#endif // MYWORK_H

mywork.cpp

#include "mywork.h"
#include <QDebug>


MyWork::MyWork(QObject *parent) : QObject(parent)
{

}

void MyWork::doWork()
{
    int num = 0;
    qDebug() << "当前线程为:" << QThread::currentThread();
    for(int i=0; i<10000000; ++i)
    {
        emit curNumber(num++);
        QThread::usleep(1);
    }
    qDebug() << "run() 执行完毕, 子线程退出...";
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mywork.h"

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

    //创建一个QThread对象
    QThread *sub = new QThread;
    //创建工作对象 --> 注意不能在构造函数中指定自己的parent
    MyWork* mywork = new MyWork;

    //将工作对象移动到线程对象中
    mywork->moveToThread(sub);

    //使用线程对象调用start方法,但是此时并没有开始执行子线程的逻辑,需要调用工作函数中的对应函数才可以
    sub->start();

    connect(ui->pushButton, &QPushButton::clicked, mywork, &MyWork::doWork);

    connect(mywork, &MyWork::curNumber, this, [=](int num){
        ui->label->setNum(num);
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值