QT线程模块使用QThtead

第一种方式:重写Qthread里的run方法

main.cpp

#include "mainwindow.h"

#include 

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    qRegisterMetaType>("QVector"); //注册 使用
    MainWindow w;

    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "mythread.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建子线程

    Generate* gen = new Generate(this);
    Quicksort* quick = new Quicksort(this);
    Bubblesort* bubble = new Bubblesort(this);

    connect(this, &MainWindow::starting, gen, &Generate::recvNum);

    connect(ui->startBtn, &QPushButton::clicked, this, [=]() {
        emit this->starting(100000);        //启动线程
        gen->start(); });
    //接受数据
    connect(gen, &Generate::sayNum, quick, &Quicksort::recvArry);
    connect(gen, &Generate::sayNum, bubble, &Bubblesort::recvArry);
    //接受数据
    connect(gen, &Generate::sayNum, this, [=](QVector v) {
        //接收到数据证明程序已经启动了 ,
        quick->start();
        bubble->start();
        //显示
        for (int i = 0; i < v.size(); ++i) {
            ui->randomList->addItem(QString::number(v.at(i)));
        }
        
    });
    //冒泡
    connect(bubble, &Bubblesort::finish, this, [=](QVector v) {
        //        qDebug()<<"冒泡显示";
        //接收到数据证明程序已经启动了
        for (int i = 0; i < v.size(); ++i) {
            ui->bubbleList->addItem(QString::number(v.at(i)));
        }
    });

    //    //快排序
    connect(quick, &Quicksort::finish, this, [=](QVector v) {
        //接收到数据证明程序已经启动了 ,
        //        qDebug() << "内置排序";
        for (int i = 0; i < v.size(); ++i) {
            ui->quickList->addItem(QString::number(v.at(i)));
        }
    });
}

void MainWindow::subThread()
{
    int num = 0;
    while (1) {
        num++;
        if (num == 100000) {
            break;
        }
        label->setText(QString::number(num));
        QThread::sleep(1);
    }
}

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

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();
    void subThread();
    QLabel* label;

private:
    Ui::MainWindow* ui;
signals:
    void starting(int num);
};
#endif // MAINWINDOW_H

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include 
#include 
class Generate : public QThread {
    Q_OBJECT
public:
    explicit Generate(QWidget* parent = nullptr);
    void recvNum(int num);

signals:
    void sayNum(QVector);

protected:
    void run() override;

private:
    int num;
};
//冒泡排序
class Bubblesort : public QThread {
    Q_OBJECT
public:
    explicit Bubblesort(QWidget* parent = nullptr);
    void recvArry(QVector);

signals:
    void finish(QVector);

protected:
    void run() override;

private:
    QVector v;
};
//快速排序

class Quicksort : public QThread {
    Q_OBJECT
public:
    explicit Quicksort(QWidget* parent = nullptr);
    void recvArry(QVector);

signals:
    void finish(QVector);

protected:
    void run() override;

private:
    QVector v;
};

#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include 
#include 
#include 

Generate::Generate(QWidget* parent)
    : QThread { parent }
{
}
void Generate::run()
{
    qDebug() << "线程运行了,线程地址是" << Generate::currentThread();

    QVector v;
    QElapsedTimer time;
    time.start();
    for (int i = 0; i < this->num; i++) {
        //        v.push_back(rand() % 10000);
        v.push_back(QRandomGenerator::global()->bounded(0, 1000));
    }
    int milsec = time.elapsed();
    qDebug() << "生成" << num << "个随机数"
             << "耗时" << milsec << "毫秒";
    emit this->sayNum(v);
}

void Generate::recvNum(int num)
{
    this->num = num;
}

//冒泡代码

Bubblesort::Bubblesort(QWidget* parent)
    : QThread(parent)
{
}

void Bubblesort::recvArry(QVector v)
{
    this->v = v;
}

void Bubblesort::run()
{

    QElapsedTimer timer;
    timer.start();
    //冒泡排序
    qDebug() << "冒泡" << QThread::currentThread();
    for (int i = 0; i < v.size(); ++i) {
        for (int j = 0; j < v.size() - i - 1; ++j) {
            if (v[j] > v[j + 1]) {
                int temp = v[j];
                v[j] = v[j + 1];
                v[j + 1] = temp;
            }
        }
    }
    int milsec = timer.elapsed();
    qDebug() << "冒泡耗时" << milsec << "毫秒";
    emit finish(v);
};

//快速排序
Quicksort::Quicksort(QWidget* parent)
    : QThread(parent)
{
}

void Quicksort::recvArry(QVector v)
{
    this->v = v;
}

void Quicksort::run()
{
    QElapsedTimer timer;
    timer.start();
    //快排序
    qDebug() << "快速排序" << QThread::currentThread();
    std::sort(v.begin(), v.end());

    int milsec = timer.elapsed();
    qDebug() << "快排耗时" << milsec << "毫秒";
    emit finish(v);
};

thread.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp \
    mythread.cpp

HEADERS += \
    mainwindow.h \
    mythread.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

mainwindow.ui / ui_mainwindow.h

/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 6.3.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QVBoxLayout *verticalLayout_2;
    QWidget *widget;
    QHBoxLayout *horizontalLayout_3;
    QGroupBox *groupBox;
    QVBoxLayout *verticalLayout;
    QListWidget *randomList;
    QGroupBox *groupBox_2;
    QHBoxLayout *horizontalLayout;
    QListWidget *bubbleList;
    QGroupBox *groupBox_3;
    QHBoxLayout *horizontalLayout_2;
    QListWidget *quickList;
    QPushButton *startBtn;
    QMenuBar *menubar;
    QStatusBar *statusbar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(800, 600);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        verticalLayout_2 = new QVBoxLayout(centralwidget);
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        widget = new QWidget(centralwidget);
        widget->setObjectName(QString::fromUtf8("widget"));
        horizontalLayout_3 = new QHBoxLayout(widget);
        horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
        groupBox = new QGroupBox(widget);
        groupBox->setObjectName(QString::fromUtf8("groupBox"));
        verticalLayout = new QVBoxLayout(groupBox);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        randomList = new QListWidget(groupBox);
        randomList->setObjectName(QString::fromUtf8("randomList"));

        verticalLayout->addWidget(randomList);


        horizontalLayout_3->addWidget(groupBox);

        groupBox_2 = new QGroupBox(widget);
        groupBox_2->setObjectName(QString::fromUtf8("groupBox_2"));
        horizontalLayout = new QHBoxLayout(groupBox_2);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        bubbleList = new QListWidget(groupBox_2);
        bubbleList->setObjectName(QString::fromUtf8("bubbleList"));

        horizontalLayout->addWidget(bubbleList);


        horizontalLayout_3->addWidget(groupBox_2);

        groupBox_3 = new QGroupBox(widget);
        groupBox_3->setObjectName(QString::fromUtf8("groupBox_3"));
        horizontalLayout_2 = new QHBoxLayout(groupBox_3);
        horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
        quickList = new QListWidget(groupBox_3);
        quickList->setObjectName(QString::fromUtf8("quickList"));

        horizontalLayout_2->addWidget(quickList);


        horizontalLayout_3->addWidget(groupBox_3);


        verticalLayout_2->addWidget(widget);

        startBtn = new QPushButton(centralwidget);
        startBtn->setObjectName(QString::fromUtf8("startBtn"));

        verticalLayout_2->addWidget(startBtn);

        MainWindow->setCentralWidget(centralwidget);
        menubar = new QMenuBar(MainWindow);
        menubar->setObjectName(QString::fromUtf8("menubar"));
        menubar->setGeometry(QRect(0, 0, 800, 26));
        MainWindow->setMenuBar(menubar);
        statusbar = new QStatusBar(MainWindow);
        statusbar->setObjectName(QString::fromUtf8("statusbar"));
        MainWindow->setStatusBar(statusbar);

        retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
        groupBox->setTitle(QCoreApplication::translate("MainWindow", "\351\232\217\346\234\272\346\225\260", nullptr));
        groupBox_2->setTitle(QCoreApplication::translate("MainWindow", "\345\206\222\346\263\241\346\216\222\345\272\217", nullptr));
        groupBox_3->setTitle(QCoreApplication::translate("MainWindow", "\345\277\253\351\200\237\346\216\222\345\272\217", nullptr));
        startBtn->setText(QCoreApplication::translate("MainWindow", "\345\274\200\345\247\213", nullptr));
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAINWINDOW_H

第二种方式:继承QObject对象

mainwindow.cpp

#include "mainwindow.h"
#include "mythread.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建子线程

    Generate* gen = new Generate;
    Quicksort* quick = new Quicksort;
    Bubblesort* bubble = new Bubblesort;
    //创建任务类
    QThread* thread1 = new QThread;
    QThread* thread2 = new QThread;
    QThread* thread3 = new QThread;
    //将任务对象移入
    gen->moveToThread(thread1);
    quick->moveToThread(thread2);
    bubble->moveToThread(thread3);

    connect(this, &MainWindow::starting, gen, &Generate::working);

    connect(ui->startBtn, &QPushButton::clicked, this, [=]() {
        emit this->starting(10000);        //启动线程
        thread1->start(); });

    //接受数据
    connect(gen, &Generate::sayNum, quick, &Quicksort::working);
    connect(gen, &Generate::sayNum, bubble, &Bubblesort::working);
    //    connect(gen, &Generate::sayNum, this, [=](QVector v) {
    //        quick->working(v);
    //        bubble->working(v);
    //    }); //错误写法,在主线程运行了

    //接受数据
    connect(gen, &Generate::sayNum, this, [=](QVector v) {
        //接收到数据证明程序已经启动了 ,
        thread2->start();
        thread3->start();
        //显示
        for (int i = 0; i < v.size(); ++i) {
            ui->randomList->addItem(QString::number(v.at(i)));
        }
    });
    //冒泡
    connect(bubble, &Bubblesort::finish, this, [=](QVector v) {
        //        qDebug()<<"冒泡显示";
        //接收到数据证明程序已经启动了
        for (int i = 0; i < v.size(); ++i) {
            ui->bubbleList->addItem(QString::number(v.at(i)));
        }
    });

    //    //快排序
    connect(quick, &Quicksort::finish, this, [=](QVector v) {
        //接收到数据证明程序已经启动了 ,
        //        qDebug() << "内置排序";
        for (int i = 0; i < v.size(); ++i) {
            ui->quickList->addItem(QString::number(v.at(i)));
        }
    });
    connect(this, &QWidget::destroyed, this, [=]() {
        thread1->quit();
        thread1->wait();
        thread1->deleteLater();

        thread2->quit();
        thread2->wait();
        thread2->deleteLater();

        thread3->quit();
        thread3->wait();
        thread3->deleteLater();
    });
}

void MainWindow::subThread()
{
    int num = 0;
    while (1) {
        num++;
        if (num == 100000) {
            break;
        }
        label->setText(QString::number(num));
        QThread::sleep(1);
    }
}

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

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();
    void subThread();
    QLabel* label;

private:
    Ui::MainWindow* ui;
signals:
    void starting(int num);
};
#endif // MAINWINDOW_H

线程池使用

继承QRunnable重写run方法,run方法是公共函数

thread.cpp

#include "mythread.h"
#include 
#include 
#include 
#include 
#include 

Generate::Generate(QWidget* parent)
    : QObject(parent)
    , QRunnable()
{
    setAutoDelete(true);
}
void Generate::run()
{
    qDebug() << "线程运行了,线程地址是" << QThread::currentThread();

    QVector v;
    QElapsedTimer time;
    time.start();
    for (int i = 0; i < this->num; i++) {
        //        v.push_back(rand() % 10000);
        v.push_back(QRandomGenerator::global()->bounded(0, 1000));
    }
    int milsec = time.elapsed();
    qDebug() << "生成" << num << "个随机数"
             << "耗时" << milsec << "毫秒";
    emit this->sayNum(v);
}

void Generate::recvNum(int num)
{
    this->num = num;
}

//冒泡代码

Bubblesort::Bubblesort(QWidget* parent)
    : QObject(parent)
    , QRunnable()

{
    setAutoDelete(true);
}

void Bubblesort::recvArry(QVector v)
{
    this->v = v;
}

void Bubblesort::run()
{

    QElapsedTimer timer;
    timer.start();
    //冒泡排序
    qDebug() << "冒泡" << QThread::currentThread();
    for (int i = 0; i < v.size(); ++i) {
        for (int j = 0; j < v.size() - i - 1; ++j) {
            if (v[j] > v[j + 1]) {
                int temp = v[j];
                v[j] = v[j + 1];
                v[j + 1] = temp;
            }
        }
    }
    int milsec = timer.elapsed();
    qDebug() << "冒泡耗时" << milsec << "毫秒";
    emit finish(v);
};

//快速排序
Quicksort::Quicksort(QWidget* parent)
    : QObject(parent)
    , QRunnable()

{
    setAutoDelete(true);
}

void Quicksort::recvArry(QVector v)
{
    this->v = v;
}

void Quicksort::run()
{
    QElapsedTimer timer;
    timer.start();
    //快排序
    qDebug() << "快速排序" << QThread::currentThread();
    std::sort(v.begin(), v.end());

    int milsec = timer.elapsed();
    qDebug() << "快排耗时" << milsec << "毫秒";
    emit finish(v);
};

thread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include 
#include 
#include 
#include 
class Generate : public QObject, public QRunnable {
    Q_OBJECT
public:
    explicit Generate(QWidget* parent = nullptr);
    void recvNum(int num);
    void run() override;

signals:
    void sayNum(QVector);

protected:
private:
    int num;
};
//冒泡排序
class Bubblesort : public QObject, public QRunnable {
    Q_OBJECT
public:
    explicit Bubblesort(QWidget* parent = nullptr);
    void recvArry(QVector);
    void run() override;

signals:
    void finish(QVector);

private:
    QVector v;
};
//快速排序

class Quicksort : public QObject, public QRunnable {
    Q_OBJECT
public:
    explicit Quicksort(QWidget* parent = nullptr);
    void recvArry(QVector);
    void run() override;

signals:
    void finish(QVector);

private:
    QVector v;
};

#endif // MYTHREAD_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();
    void subThread();
    QLabel* label;

private:
    Ui::MainWindow* ui;
signals:
    void starting(int num);
};
#endif // MAINWINDOW_H

maindindow.cpp

#include "mainwindow.h"
#include "mythread.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创任务类;

    Generate* gen = new Generate;
    Quicksort* quick = new Quicksort;
    Bubblesort* bubble = new Bubblesort;

    connect(this, &MainWindow::starting, gen, &Generate::recvNum);

    connect(ui->startBtn, &QPushButton::clicked, this, [=]() {
        emit this->starting(100000); //启动线程
        //        gen->start();
        QThreadPool::globalInstance()->start(gen);
    });
    //接受数据
    connect(gen, &Generate::sayNum, quick, &Quicksort::recvArry);
    connect(gen, &Generate::sayNum, bubble, &Bubblesort::recvArry);
    //接受数据
    connect(gen, &Generate::sayNum, this, [=](QVector v) {
        //接收到数据证明程序已经启动了 ,
        //        quick->start();
        //        bubble->start();
        QThreadPool::globalInstance()->start(quick);
        QThreadPool::globalInstance()->start(bubble);

        //显示
        for (int i = 0; i < v.size(); ++i) {
            ui->randomList->addItem(QString::number(v.at(i)));
        }
    });
    //冒泡
    connect(bubble, &Bubblesort::finish, this, [=](QVector v) {
        //        qDebug()<<"冒泡显示";
        //接收到数据证明程序已经启动了
        for (int i = 0; i < v.size(); ++i) {
            ui->bubbleList->addItem(QString::number(v.at(i)));
        }
    });

    //    //快排序
    connect(quick, &Quicksort::finish, this, [=](QVector v) {
        //接收到数据证明程序已经启动了 ,
        //        qDebug() << "内置排序";
        for (int i = 0; i < v.size(); ++i) {
            ui->quickList->addItem(QString::number(v.at(i)));
        }
    });
}

void MainWindow::subThread()
{
    int num = 0;
    while (1) {
        num++;
        if (num == 100000) {
            break;
        }
        label->setText(QString::number(num));
        QThread::sleep(1);
    }
}

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

你可能感兴趣的:(qt,java,linux)