Qt多线程Qt中多线程的使用
苏丙榅在进行桌面应用程序开发的时候, 假设应用程序在某些情况下需要处理比较复杂的逻辑, 如果只有一个线程去处理,就会导致窗口卡顿,无法处理用户的相关操作。这种情况下就需要使用多线程,其中一个线程处理窗口事件,其他线程进行逻辑运算,多个线程各司其职,不仅可以提高用户体验还可以提升程序的执行效率。
在qt中使用了多线程,有些事项是需要额外注意的:
- 默认的线程在Qt中称之为窗口线程,也叫主线程,负责窗口事件处理或者窗口控件数据的更新
- 子线程负责后台的业务逻辑处理,子线程中不能对窗口对象做任何操作,这些事情需要交给窗口线程处理
- 主线程和子线程之间如果要进行数据的传递,需要使用Qt中的信号槽机制
1. 线程类 QThread
Qt中提供了一个线程类,通过这个类就可以创建子线程了,Qt中一共提供了两种创建子线程的方式,后边会依次介绍其使用方式。先来看一下这个类中提供的一些常用API函数:
1.1 常用共用成员函数
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 | 
 QThread::QThread(QObject *parent = Q_NULLPTR);
 
 bool QThread::isFinished() const;
 
 bool QThread::isRunning() const;
 
 
 
 Priority QThread::priority() const;
 void QThread::setPriority(Priority priority);
 优先级:
 QThread::IdlePriority         --> 最低的优先级
 QThread::LowestPriority
 QThread::LowPriority
 QThread::NormalPriority
 QThread::HighPriority
 QThread::HighestPriority
 QThread::TimeCriticalPriority --> 最高的优先级
 QThread::InheritPriority      --> 子线程和其父线程的优先级相同, 默认是这个
 
 
 void QThread::exit(int returnCode = 0);
 
 
 bool QThread::wait(unsigned long time = ULONG_MAX);
 
 | 
1.2 信号槽
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | 
 [slot] void QThread::quit();
 
 [slot] void QThread::start(Priority priority = InheritPriority);
 
 [slot] void QThread::terminate();
 
 
 
 [signal] void QThread::finished();
 
 [signal] void QThread::started();
 
 | 
1.3 静态函数
| 12
 3
 4
 5
 6
 7
 8
 
 | [static] QThread *QThread::currentThread();
 
 [static] int QThread::idealThreadCount();
 
 [static] void QThread::msleep(unsigned long msecs);
 [static] void QThread::sleep(unsigned long secs);
 [static] void QThread::usleep(unsigned long usecs);
 
 | 
1.4 任务处理函数
| 12
 
 | [virtual protected] void QThread::run();
 
 | 
这个run()是一个虚函数,如果想让创建的子线程执行某个任务,需要写一个子类让其继承QThread,并且在子类中重写父类的run()方法,函数体就是对应的任务处理流程。另外,这个函数是一个受保护的成员函数,不能够在类的外部调用,如果想要让线程执行这个函数中的业务流程,需要通过当前线程对象调用槽函数start()启动子线程,当子线程被启动,这个run()函数也就在线程内部被调用了。
2. 使用方式1
2.1 操作步骤
Qt中提供的多线程的第一种使用方式的特点是: 简单。操作步骤如下:
- 需要创建一个线程类的子类,让其继承QT中的线程类 QThread,比如: | 12
 3
 4
 
 | class MyThread:public QThread{
 ......
 }
 
 |  
 
- 重写父类的 run() 方法,在该函数内部编写子线程要处理的具体的业务流程 | 12
 3
 4
 5
 6
 7
 8
 9
 
 | class MyThread:public QThread{
 ......
 protected:
 void run()
 {
 ........
 }
 }
 
 |  
 
- 在主线程中创建子线程对象,new 一个就可以了 | 1
 | MyThread * subThread = new MyThread;
 |  
 
- 启动子线程, 调用 start() 方法 
  不能在类的外部调用run() 方法启动子线程,在外部调用start()相当于让run()开始运行
当子线程别创建出来之后,父子线程之间的通信可以通过信号槽的方式,注意事项: 
- 在Qt中在子线程中不要操作程序中的窗口类型对象, 不允许, 如果操作了程序就挂了         
- 只有主线程才能操作程序中的窗口对象, 默认的线程就是主线程, 自己创建的就是子线程
2.2 示例代码
举一个简单的数数的例子,假如只有一个线程,让其一直数数,会发现数字并不会在窗口中时时更新,并且这时候如果用户使用鼠标拖动窗口,就会出现无响应的情况,使用多线程就不会出现这样的现象了。
在下面的窗口中,点击按钮开始在子线程中数数,让后通过信号槽机制将数据传递给UI线程,通过UI线程将数据更新到窗口中。
 
mythread.h
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | #ifndef MYTHREAD_H#define MYTHREAD_H
 
 #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.cpp
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | #include "mythread.h"#include <QDebug>
 
 MyThread::MyThread(QObject *parent) : QThread(parent)
 {
 
 }
 
 void MyThread::run()
 {
 qDebug() << "当前线程对象的地址: " << QThread::currentThread();
 
 int num = 0;
 while(1)
 {
 emit curNumber(num++);
 if(num == 10000000)
 {
 break;
 }
 QThread::usleep(1);
 }
 qDebug() << "run() 执行完毕, 子线程退出...";
 }
 
 | 
mainwindow.cpp
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | #include "mainwindow.h"#include "ui_mainwindow.h"
 #include "mythread.h"
 #include <QDebug>
 
 MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent),
 ui(new Ui::MainWindow)
 {
 ui->setupUi(this);
 
 qDebug() << "主线程对象地址:  " << QThread::currentThread();
 
 MyThread* subThread = new MyThread;
 
 connect(subThread, &MyThread::curNumber, this, [=](int num)
 {
 ui->label->setNum(num);
 });
 
 connect(ui->startBtn, &QPushButton::clicked, this, [=]()
 {
 
 subThread->start();
 });
 }
 
 MainWindow::~MainWindow()
 {
 delete ui;
 }
 
 | 
这种在程序中添加子线程的方式是非常简单的,但是也有弊端,假设要在一个子线程中处理多个任务,所有的处理逻辑都需要写到run()函数中,这样该函数中的处理逻辑就会变得非常混乱,不太容易维护。
3. 使用方式2
3.1 操作步骤
Qt提供的第二种线程的创建方式弥补了第一种方式的缺点,用起来更加灵活,但是这种方式写起来会相对复杂一些,其具体操作步骤如下:
- 创建一个新的类,让这个类从QObject派生
| 12
 3
 4
 
 | class MyWork:public QObject{
 .......
 }
 
 | 
- 在这个类中添加一个公共的成员函数,函数体就是我们要子线程中执行的业务逻辑 | 12
 3
 4
 5
 6
 7
 
 | class MyWork:public QObject{
 public:
 .......
 
 void working();
 }
 
 |  
 
- 在主线程中创建一个QThread对象, 这就是子线程的对象 
| 1
 | QThread* sub = new QThread;
 | 
- 在主线程中创建工作的类对象(千万不要指定给创建的对象指定父对象)
| 12
 
 | MyWork* work = new MyWork(this);    MyWork* work = new MyWork;
 
 | 
- 将MyWork对象移动到创建的子线程对象中, 需要调用QObject类提供的moveToThread()方法
| 12
 3
 4
 
 | 
 
 work->moveToThread(sub);
 
 | 
- 启动子线程,调用 - start(), 这时候线程启动了, 但是移动到线程中的对象并没有工作
 
- 调用MyWork类对象的工作函数,让这个函数开始执行,这时候是在移动到的那个子线程中运行的 
3.2 示例代码
假设函数处理上面在程序中数数的这个需求,具体的处理代码如下:
mywork.h
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | #ifndef MYWORK_H#define MYWORK_H
 
 #include <QObject>
 
 class MyWork : public QObject
 {
 Q_OBJECT
 public:
 explicit MyWork(QObject *parent = nullptr);
 
 
 void working();
 
 signals:
 void curNumber(int num);
 
 public slots:
 };
 
 #endif
 
 | 
mywork.cpp
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | #include "mywork.h"#include <QDebug>
 #include <QThread>
 
 MyWork::MyWork(QObject *parent) : QObject(parent)
 {
 
 }
 
 void MyWork::working()
 {
 qDebug() << "当前线程对象的地址: " << QThread::currentThread();
 
 int num = 0;
 while(1)
 {
 emit curNumber(num++);
 if(num == 10000000)
 {
 break;
 }
 QThread::usleep(1);
 }
 qDebug() << "run() 执行完毕, 子线程退出...";
 }
 
 | 
mainwindow.cpp
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 
 | #include "mainwindow.h"#include "ui_mainwindow.h"
 #include <QThread>
 #include "mywork.h"
 #include <QDebug>
 
 MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent),
 ui(new Ui::MainWindow)
 {
 ui->setupUi(this);
 
 qDebug() << "主线程对象的地址: " << QThread::currentThread();
 
 
 QThread* sub = new QThread;
 
 
 
 MyWork* work = new MyWork;
 
 work->moveToThread(sub);
 
 sub->start();
 
 connect(ui->startBtn, &QPushButton::clicked, work, &MyWork::working);
 
 connect(work, &MyWork::curNumber, this, [=](int num)
 {
 ui->label->setNum(num);
 });
 }
 
 MainWindow::~MainWindow()
 {
 delete ui;
 }
 
 | 
使用这种多线程方式,假设有多个不相关的业务流程需要被处理,那么就可以创建多个类似于MyWork的类,将业务流程放多类的公共成员函数中,然后将这个业务类的实例对象移动到对应的子线程中moveToThread()就可以了,这样可以让编写的程序更加灵活,可读性更强,更易于维护。