# 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();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QThread是Qt框架中用于多线程编程的类。通过继承QThread类,可以创建自定义的线程类,并在其中实现自己的线程逻辑。下面是一个使用QThread的示例代码: 在ThreadController.hpp中,ThreadController类继承自QObject,并包含一个QThread对象workerThread。在构造函数中,创建了一个ThreadWorker对象threadWork,并将其移交给workerThread线程。通过QObject::connect函数连接了一个信号和槽函数,当发出touchWork信号时,会触发threadWork的work槽函数。然后启动workerThread线程,并发出touchWork信号,从而触发线程执行工作。 在main.cpp中,创建了一个QCoreApplication对象a,并打印了主线程的ID。然后创建了一个CustomThread对象customThread,并通过QObject::connect函数连接了一个信号和槽函数。最后调用customThread的start函数启动线程。 在CustomThread.hpp中,CustomThread类继承自QThread。在run函数中,打印了当前线程的ID,并通过QThread::sleep函数模拟了线程的工作。然后发出customThreadSignal信号,从而触发customThreadSlot槽函数。 综上所述,QThread的使用方法是通过继承QThread类,重写run函数,在其中实现线程的逻辑。可以通过信号和槽函数来与其他线程进行通信。 #### 引用[.reference_title] - *1* *2* *3* [QT 线程QThread使用方式](https://blog.csdn.net/weixin_41111116/article/details/126372972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值