alin的学习之路:Qt与多线程
如果程序在进行复杂的逻辑处理过程中, 对窗口进行操作, 就会出现无响应的情况。
如何解决这样的问题与高并发的问题?
需要使用多线程。
方式1
特点:简单
-
创建一个自定义的类,先选择继承QObject,随后改为继承QThread
-
重写父类的run()方法,在run()方法里面写子线程的执行逻辑。注意run函数是protected的作用域
[virtual protected] void QThread::run()
-
在主线程中创建子线程的对象
-
使用子线程对象调用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
特点:灵活
- 创建一个工作类,定义一个工作函数,这个工作函数就是子线程的执行代码。
- 主线程中创建一个工作类的对象
- 千万不要指定给创建的对象指定父对象
- 主线程中创建一个QThread类的对象
- 将工作类对象移动到创建的线程对象中,使用QObject类提供的api, moveToThread
- void QObject::moveToThread(QThread *targetThread);
- work->moveToThread(sub); // 移动到子线程中工作
- 启动子线程,调用线程对象中的start()方法,此时并没有开始执行子线程的代码逻辑
- 调用工作对象的工作函数,此时子线程开始执行
线程间通信:
需要使用槽函数
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;
}