1.QThread方法创建线程时的注意点
继承QThread创建子线程的注意点:
(1)需要写一个继承QThread的子类,然后必须要重写继承的run()函数(在run函数里面重写要在线程中执行的方法(任务函数))
(2)创建这个子类的实例对象,然后调start()方法就可以让线程工作了
缺点:(1)如果要在一个继承QThread的子类里面重写run函数时,如果想一个run函数实现多个任务函数,那么这个run函数写起来就很复杂,不利于代码的可读性
2.QThread方法创建线程代码例子
使用QThread方法实现点击开始按钮生成10000个随机数,然后分别使用冒泡排序和快速排序排序这10000个随机数,最后在窗口显示排序后的数字:
mainwindow.h文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void starting(int num);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mythread.h文件:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
//生成随机数
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
void recvNum(int num);
protected:
void run() override;
signals:
void sendArray(QVector<int> list);
private:
int m_num;
};
class BubbleSort : public QThread
{
Q_OBJECT
public:
explicit BubbleSort(QObject *parent = nullptr);
void recvArray(QVector<int> list);
protected:
void run() override;
signals:
void finish(QVector<int> num);
private:
QVector<int> m_list;
};
class QuickSort : public QThread
{
Q_OBJECT
public:
explicit QuickSort(QObject *parent = nullptr);
void recvArray(QVector<int> list);
private:
void quickSort(QVector<int> &list, int l, int r);
protected:
void run() override;
signals:
void finish(QVector<int> num);
private:
QVector<int> m_list;
};
#endif // MYTHREAD_H
mian.cpp文件:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qRegisterMetaType<QVector<int>>("QVector<int>");
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//1.创建子线程的对象
MyThread *gen = new MyThread;
BubbleSort *bubble = new BubbleSort;
QuickSort *quick = new QuickSort;
connect(this, &MainWindow::starting, gen, &MyThread::recvNum);
//2.启动子线程
connect(ui->start, &QPushButton::clicked, this, [=]{
emit starting(10000); //在启动子线程时,将要生成的随机数的个数发送出去
gen->start();
});
//接收子线程发送的数据
connect(gen, &MyThread::sendArray, bubble, &BubbleSort::recvArray);
connect(gen, &MyThread::sendArray, quick, &QuickSort::recvArray);
connect(gen, &MyThread::sendArray, this, [=](QVector<int> list){ //connect里面不支持传递QVector类型//需要使用qRegisterMetaType()进行注册
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){ //connect里面不支持传递QVector类型//需要使用qRegisterMetaType()进行注册
for(int i = 0; i < list.size(); i++){
ui->bubbleList->addItem(QString::number(list.at(i)));
}
});
connect(quick, &QuickSort::finish, this, [=](QVector<int> list){ //connect里面不支持传递QVector类型//需要使用qRegisterMetaType()进行注册
for(int i = 0; i < list.size(); i++){
ui->quickList->addItem(QString::number(list.at(i)));
}
});
//窗口关闭时,释放资源
//窗口关闭时,会发送一个destroyed()信号,就可以根据这个信号来销毁线程的资源
connect(this, &MainWindow::destroyed, this,[=]{
gen->quit();
gen->wait();
gen->deleteLater(); //delete gen 销毁线程gen的资源
bubble->quit();
bubble->wait();
bubble->deleteLater(); //delete bubble 销毁线程t2的资源
quick->quit();
quick->wait();
quick->deleteLater(); //delete quick 销毁线程quick的资源
});
\
\
}
MainWindow::~MainWindow()
{
delete ui;
}
mythread.cpp文件:
#include "mythread.h"
#include <QVector>
#include <QElapsedTimer> //计算某个流程执行所使用的时间
#include <QDebug>
#include <QThread>
MyThread::MyThread(QObject *parent)
: QThread(parent)
{
}
void MyThread::recvNum(int num)
{
m_num = num;
}
void MyThread::run()
{
qDebug() << "生成随机数的线程地址" << QThread::currentThread(); //获取一个指针,这个指针指向当前线程对象的地址
QVector<int> list;
QElapsedTimer time;
time.start();
for(int i = 0; i < m_num; i++){
list.push_back(qrand() % 100000);
}
int milsec = time.elapsed();
qDebug() << "生成" << m_num << "个随机数总共用时:" << milsec << "毫秒";
emit sendArray(list);
}
BubbleSort::BubbleSort(QObject *parent)
:QThread(parent)
{
}
void BubbleSort::recvArray(QVector<int> list)
{
m_list = list;
}
void BubbleSort::run()
{
qDebug() << "冒泡排序的线程地址" << QThread::currentThread(); //获取一个指针,这个指针指向当前线程对象的地址
QElapsedTimer time;
time.start();
for(int i = 0; i < m_list.size() - 1 ;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)
{
}
void QuickSort::recvArray(QVector<int> list)
{
m_list = list;
}
void QuickSort::quickSort(QVector<int> &s, int l, int r)
{
if(l < r){
int i = l, j = r;
int x = s[l];
while(i < j){
while(i < j &&s[j] >=x){
j--;
}
if(i < j){
s[i++] = s[j];
}
while(i < j && s[i] < x){
i++;
}
if(i < j){
s[j--] = s[i];
}
}
s[i] = x;
quickSort(s, l, i - 1);
quickSort(s, i + 1, r);
}
}
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);
}
运行结果: