使用的是VS2019+QT5.14.2 ,QT版本不同可能需要更改头文件
学自于B站,视频链接如下
https://www.bilibili.com/video/BV1iN411f7dY?p=8&spm_id_from=pageDriver
此博主博文地址如下,可具体查看QT线程的使用方式
https://subingwen.cn/qt/
下面通过例程进行学习
QThread使用方式一
程序需要实现如下功能:
除了主线程外,创建随机数生成、冒泡排序、快速排序三个子线程,并将计算结果分别显示,可以得到每个线程花费时间,如下。
main.cpp
#include "QtThread.h"
#include <QtWidgets/QApplication>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
//需要注册的原因可参考https://blog.csdn.net/wadfji/article/details/54406767
//传递QVector<int>时需要注册
qRegisterMetaType<QVector<int>>("QVector<int>");
QtThread w;
w.show();
return a.exec();
}
QtThread.h创建工程时起的类名
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtThread.h"
class QtThread : public QMainWindow
{
Q_OBJECT
public:
QtThread(QWidget* parent = Q_NULLPTR);
signals:
void starting(int num);//点击按钮发送开始信号
private:
Ui::QtThreadClass ui;
};
QtThread.cpp
#include "QtThread.h"
#include "mythread.h"
QtThread::QtThread(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//1.创建生成随机数的子线程对象
Generate* gen = new Generate;
//创建排序类的对象
BubbleSort* bubble = new BubbleSort;
QuickSort* quick = new QuickSort;
//主线程发送需要的随机数个数给生成随机数的子线程
connect(this, &QtThread::starting, gen, &Generate::recvNum);
//2.启动子线程,生成随机数子线程开始运行
connect(ui.btn_start, &QPushButton::clicked, this, [=]() {
emit starting(10000);
gen->start();
});
//3.生成随机数的子线程把数据发送给排序的两个子线程和主线程
connect(gen, &Generate::sendArray, bubble, &BubbleSort::recvArray); //排序线程接收数据
connect(gen, &Generate::sendArray, quick, &QuickSort::recvArray);
connect(gen, &Generate::sendArray, this, [=](QVector<int> list) {
//排序线程开始运行
bubble->start();
quick->start();
//显示随机生成的数
for (int i = 0; i < list.size(); i++)
{
ui.randList->addItem(QString::number(list.at(i)));
}
});
//主线程接收排序后的结果
connect(bubble, &BubbleSort::finish, this, [=](QVector<int> list) {
for (int i = 0; i < list.size(); i++)
{
ui.bubbleList->addItem(QString::number(list.at(i)));
}
});
connect(quick, &QuickSort::finish, this, [=](QVector<int> list) {
for (int i = 0; i < list.size(); i++)
{
ui.quickList->addItem(QString::number(list.at(i)));
}
});
//内存回收,当前窗口关闭的时候会发送destroyed信号
connect(this, &QtThread::destroyed, this, [=]() {
gen->quit(); gen->wait(); gen->deleteLater();
bubble->quit(); bubble->wait(); bubble->deleteLater();
quick->quit(); quick->wait(); quick->deleteLater();
});
}
mythread.h包含三个子线程
#pragma once
#include <QThread>
#include <qvector.h>
class Generate : public QThread
{
Q_OBJECT
public:
explicit Generate(QObject* parent = nullptr);
~Generate();
void recvNum(int num);
protected:
void run() override;
signals:
void sendArray(QVector<int> num);
private:
int m_num;
};
//*****************冒泡排序*********************
class BubbleSort : public QThread
{
Q_OBJECT
public:
explicit BubbleSort(QObject* parent = nullptr);
~BubbleSort();
void recvArray(QVector<int> list);
protected:
void run() override;
signals:
void finish(QVector<int> list);
private:
QVector<int> m_list;
};
//********************快速排序*************************
class QuickSort : public QThread
{
Q_OBJECT
public:
explicit QuickSort(QObject* parent = nullptr);
~QuickSort();
void recvArray(QVector<int> list);
protected:
void run() override;
private:
void quickSort(QVector<int>& list, int l, int r);
signals:
void finish(QVector<int> list);
private:
QVector<int> m_list;
};
mythread.cpp
#include "mythread.h"
#include <qelapsedtimer.h>
#include <qdebug.h>
//*******************生成随机数******************
Generate::Generate(QObject* parent)
: QThread(parent)
{
}
Generate::~Generate()
{
}
void Generate::run()
{
qDebug() << "生成随机数的线程的线程地址:" << QThread::currentThread();
QVector<int> list;
QElapsedTimer time;
time.start();
for (int i = 0; i < this->m_num; i++)
{
list.push_back(qrand() % 100000);
}
int milsec = time.elapsed();
qDebug() << "生成" << this->m_num << "个随机数总共用时:" << milsec << "毫秒";
emit sendArray(list); //发送信号给主线程
}
void Generate::recvNum(int num)
{
this->m_num = num;
}
//*****************冒泡排序**********************
BubbleSort::BubbleSort(QObject* parent) :QThread(parent)
{
}
BubbleSort::~BubbleSort()
{
}
void BubbleSort::recvArray(QVector<int> list)
{
this->m_list = list;
}
void BubbleSort::run()
{
qDebug() << "冒泡排序的线程地址:" << QThread::currentThread();
QElapsedTimer time;
time.start();
for (int i = 0; i < m_list.size(); i++)
{
for (int j = 0; j < m_list.size() - i - 1; j++)
{
if (m_list[j] > m_list[j + 1])
{
int temp = m_list[j];
m_list[j] = m_list[j + 1];
m_list[j + 1] = temp;
}
}
}
int milsec = time.elapsed();
qDebug() << "冒泡排序总共用时:" << milsec << "毫秒";
emit finish(m_list); //发送信号给主线程
}
//********************快速排序**************************
QuickSort::QuickSort(QObject* parent) :QThread(parent)
{
}
QuickSort::~QuickSort()
{
}
//快速排序
void QuickSort::quickSort(QVector<int>& s, int l, int r)
{
if (l < r)
{
int i = l, j = r;
//拿出第一个元素保存到x中,第一个位置成为一个坑
int x = s[l];
while (i < j)
{
//从右向左找小于x的数
while (i < j && s[j] >= x)
{
//左移,直到遇到小于等于x的数
j--;
}
if (i < j)
{
//将右侧找到的小于x的元素放入左侧坑中,右侧出现一个坑
//左侧元素索引后移
s[i++] = s[j];
}
//从左向右找大于等于x的数
while (i < j && s[i] < x)
{
//右移,直到遇到大于x的数
i++;
}
if (i < j)
{
//将左侧找到的元素放入右侧坑中,左侧出现一个坑
//右侧元素索引向前移动
s[j--] = s[i];
}
}
//此时,i=j,将保存在x中的数填入坑中
s[i] = x;
quickSort(s, l, i - 1);
quickSort(s, i + 1, r);
}
}
//接收数据
void QuickSort::recvArray(QVector<int> list)
{
m_list = list;
}
void QuickSort::run()
{
qDebug() << "快速排序的线程地址:" << QThread::currentThread();
QElapsedTimer time;
time.start();
quickSort(m_list, 0, m_list.size() - 1);
int milsec = time.elapsed();
qDebug() << "快速排序总共用时:" << milsec << "毫秒";
emit finish(m_list); //发送排序完的数据给主线程
}
网友评论