Qt QThread的moveToThread方法使用

Qt QThread的moveToThread方法使用

从Qt4.4版本之后,因为QThread的run方法创建新线程这样实现与Qt设计的理念不符,Qt主推使用moveToThread方法来创建新线程。QThread 应该被看做是操作系统线程的接口或控制点,而不应该包含需要在新线程中运行的代码。需要运行的代码应该放到一个QObject的子类中,然后将该子类的对象moveToThread到新线程中。主要操作步骤如下:

  1. 创建一个类对象,创建一个线程对象。
  2. 创建与类对象链接的信号槽。
  3. 通过类对象的moveToThread方法将类对象移动到新线程中。
  4. 调用线程对象的start方法,启动线程。
  5. 调用信号槽,类对象在新线程中处理数据。

具体代码如下:

1:要放入新线程的Worker类

h文件:

#ifndef WORKER_H
#define WORKER_H

#include <QObject>

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr);

    void do_something();

signals:

};

#endif // WORKER_H

cpp文件:

#include <QtDebug>
#include <QThread>
#include "worker.h"

Worker::Worker(QObject *parent)
    : QObject(parent)
{

}

void Worker::do_something()
{
    qDebug()<< "Worker: " << (quint64) QThread::currentThreadId();
}

2:操纵Worker类对象的Print_thread类

h文件:

#ifndef PRINT_THREAD_H
#define PRINT_THREAD_H

#include <QObject>

QT_BEGIN_NAMESPACE
class QThread;
class Worker;
QT_END_NAMESPACE

/*****************************************************************************************
  @copyright qiaowei
  @author    qiaowei
  @version   1.0
  @date      2020-07-25
  @brief     创建新线程,并将Worker类对象放入线程中。打印Print_thread、Worker对象所在的线程
******************************************************************************************/

class Print_thread : public QObject
{
    Q_OBJECT
public:
    explicit Print_thread(QObject *parent = nullptr);
    ~Print_thread();

    void work_start();

signals:
    void signal_do_something();

private:
    QThread* thread_;
    Worker* worker_;
};

#endif // PRINT_THREAD_H

cpp文件:

#include <QThread>
#include <QtDebug>
#include "print_thread.h"
#include "worker.h"

Print_thread::Print_thread(QObject *parent)
    : QObject(parent)
    , thread_(new QThread())
{

}

Print_thread::~Print_thread()
{
    thread_->quit();
    thread_->wait();

    delete worker_;
    delete thread_;

    qDebug()<< "Print_thread::~Print_thread: " << (quint64) QThread::currentThreadId();
}

void Print_thread::work_start()
{
    qDebug()<< "Print_thread::work_start: " << (quint64) QThread::currentThreadId();
    worker_ = new Worker();
    worker_->moveToThread(thread_);

    connect(this, &Print_thread::signal_do_something,
            worker_, &Worker::do_something);

    thread_->start();

    emit signal_do_something();

    QThread::currentThread()->sleep(1);
}

main文件:

#include <QCoreApplication>
#include <QApplication>
#include <QtDebug>
//#include <QTextBlock>
//#include <QTextCursor>

// Print_thread放在thread_test文件夹
#include "thread_test/print_thread.h"

int main(int argc, char *argv[])
{
//    QCoreApplication a(argc, argv);
    QApplication a(argc, argv);

    qDebug()<< "Main thread: " << (quint64) QThread::currentThreadId();
    Print_thread* thread_(new Print_thread());
    thread_->work_start();

    delete thread_;

    return a.exec();
}

运行结果,打印Print_thread、Worker对象的线程号:

可以看到打印结果,Worker对象在线程6092,Print_thread、主程序入口都在线程13184

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » Qt QThread的moveToThread方法使用