# 22-QThread的用法

一、继承QThread实现多线程

#ifndef WORKER_H
#define WORKER_H

#include <QThread>
#include <QDebug>

class Worker : public QThread
{
    Q_OBJECT
public:
    Worker();

signals:
    void Finished(int value);

private:
    void run() override;
};

#endif // WORKER_H

#include "worker.h"

Worker::Worker()
{
    qDebug() << "Worker::Worker() Thread ID = " << QThread::currentThreadId();
}

void Worker::run() {

    qDebug() << "Worker::run() Thread ID = " << QThread::currentThreadId();

    for (int a = 1000; a > 0; a --) {
        for (int b = 1000; b > 0; b --) {

        }
    }
    emit Finished(3);
}

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QObject>
#include <QDebug>
#include "worker.h"

class Controller : public QObject
{
    Q_OBJECT
public:
    Controller();
    ~Controller();

    void StartProcess();

private slots:
    void Result(int value);

private:
    Worker *worker_p;
};

#endif // CONTROLLER_H

#include "controller.h"

Controller::Controller()
{
    worker_p = new Worker();
    connect(worker_p, &Worker::Finished, this, &Controller::Result);
}

Controller::~Controller() {
    delete worker_p;
}

void Controller::StartProcess() {
    qDebug() << "Controller::StartProcess() thread ID = " << QThread::currentThreadId();
    worker_p->start();
}

void Controller::Result(int value) {
    qDebug() << "Controller::Result  = " << value << " thread Id = " << QThread::currentThreadId();
}

#include <QCoreApplication>
#include <QDebug>
#include "controller.h"

int main(int argc, char *argv[])
{
    qDebug()<<"Main Thread ID = "<<QThread::currentThreadId();
    QCoreApplication a(argc, argv);
    Controller c;
    c.StartProcess();
    return a.exec();
}

二、QObject::moveToThread()实现多线程

#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include <QDebug>
#include <QThread>

class Worker : public QObject
{
    Q_OBJECT
public:
    Worker();

signals:
    void Finished(int value);

public:
    void DoWork();
};

#endif // WORKER_H

#include "worker.h"

Worker::Worker()
{
    qDebug() << "Worker::Worker() Thread ID = " << QThread::currentThreadId();
}

void Worker::DoWork() {
    qDebug() << "Worker::DoWork Thread ID = " << QThread::currentThreadId();

    for (int a = 1000; a > 0; a --) {
        for (int b = 1000; b > 0; b --) {

        }
    }

    emit Finished(3);
}


#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QObject>
#include <QThread>
#include <QDebug>

#include "worker.h"

class Controller : public QObject
{
    Q_OBJECT
public:
    Controller();
    ~Controller();

    void StartProcess();

private slots:
    void Result(int value);

private:
    QThread *thread_p;
};

#endif // CONTROLLER_H

#include "controller.h"

Controller::Controller()
{
    thread_p = new QThread();
    Worker *worker = new Worker();
    worker->moveToThread(thread_p);
    connect(worker, &Worker::Finished, this, &Controller::Result);

    connect(thread_p, &QThread::started, worker, &Worker::DoWork);
    connect(thread_p, &QThread::finished, worker, &QObject::deleteLater);
}

Controller::~Controller()
{
    thread_p->quit();
    thread_p->wait();
    delete thread_p;
}

void Controller::StartProcess() {
    qDebug() << "Controller::StartProcess() thread ID = " << QThread::currentThreadId();
    thread_p->start();
}

void Controller::Result(int value) {
    qDebug() << "Controller::Result  = " << value << " thread Id = " << QThread::currentThreadId();
}

#include <QCoreApplication>
#include <QDebug>
#include "controller.h"

int main(int argc, char *argv[])
{
    qDebug()<<"Main Thread ID = "<<QThread::currentThreadId();
    QCoreApplication a(argc, argv);
    Controller c;
    c.StartProcess();
    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值