亲宝软件园·资讯

展开

QT线程QThread

愤怒的小黄鸭 人气:0

1. 概述

QThread 有两种使用方式

  1. QObject::moveToThread()
  2. 派生QThread的子类类

2. moveThread 示例

步骤概述:

ThreadWorker.hpp代码如下:

#ifndef THREADWORKER_HPP
#define THREADWORKER_HPP
#include <QObject>
#include <QString>
#include <QThread>
#include <QDebug>
class ThreadWorker:public QObject
{
    Q_OBJECT
public:
    ThreadWorker() {}
public slots:
    void work(QString p1)
    {
        qDebug() << "current thread ID:" << QThread::currentThreadId();
        qDebug() << p1;
        QThread::sleep(10);
        qDebug() << "thread run finish!";
    }
};
#endif // THREADWORKER_HPP

ThreadController.hpp代码如下:

#ifndef THREADCONTROLLER_H
#define THREADCONTROLLER_H
#include "ThreadWorker.hpp"
class ThreadController:public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    ThreadController():QObject()
    {
        ThreadWorker* threadWork = new ThreadWorker();
        // 将 threadWork 移交给 workerThread
        threadWork->moveToThread(&workerThread);
        QObject::connect(this,SIGNAL(touchWork(QString)),threadWork,SLOT(work(QString)));
        QObject::connect(&workerThread,&QThread::finished,threadWork,&QObject::deleteLater);
        workerThread.start();                //启动线程
        qDebug()<<"current thread ID:"<<QThread::currentThreadId()<<'\n';
        emit touchWork("working");
    }
    ~ThreadController()
    {
        workerThread.quit();
        workerThread.wait();
    }
signals:
    // 发出信号触发线程
    void touchWork(QString p1);
};
#endif // THREADCONTROLLER_H

main.cpp代码如下:

#include <QCoreApplication>
#include "ThreadController.hpp"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    ThreadController tc = ThreadController();
    return a.exec();
}

注意:

不能再cpp文件中使用QT的特性机制如信号槽,因为moc不会在cpp文件中处理这些机制。可以改,但比较麻烦,建议将类定义在头文件中即可。

3. QThread 示例

方法概述:

CustomThread.hpp代码如下

#ifndef CUSTOMTHREAD_H
#define CUSTOMTHREAD_H
#include <QThread>
#include <QDebug>
class CustomThread:public QThread
{
    Q_OBJECT
public:
    CustomThread() {}
signals:
    void customThreadSignal();
public slots:
    void customThreadSlot()
    {
        qDebug()<<"current thread ID(in slot function):"<<QThread::currentThreadId()<<'\n';
    }
protected:
    void run() override
    {
        qDebug()<<"current thread ID:"<<QThread::currentThreadId()<<'\n';
        QThread::sleep(10);
        qDebug() << "thread run finish!";
        emit customThreadSignal();
    }
};
#endif // CUSTOMTHREAD_H

main.cpp代码如下

#include <QCoreApplication>
#include "CustomThread.hpp"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "main thread ID:" << QThread::currentThreadId();
    CustomThread customThread;
    QObject::connect(&customThread,&CustomThread::customThreadSignal,&customThread,&CustomThread::customThreadSlot);
    customThread.start();
    return a.exec();
}

输出结果:

main thread ID: 0x6508
current thread ID: 0x6544 

thread run finish!
current thread ID(in slot function): 0x6508 

4. 总结

  1. moveToThread

此方式,要求把需要进行的工作全部封装在一个类中,将每一个任务定义为一个槽函数,并与之对应的信号进行关联,最后调用moveToThread将此类交QThread对象。QThread调用start()进行启动,之后每个任务由相应的信号进行触发然后执行。

  1. QThread

此方式是要求基于QThread进行派生,对派生类进行run()函数的override。之后调用start()后,就会运行run()函数。但是在派生类中定义的槽函数,不会由派生类自身所执行,而是由该线程的拥有者执行。

QThread只有run函数是在新线程里执行,其他所有函数都在QThread生成的线程里执行

官方是比较推荐使用moveToThread的方式,不过也看各自的使用场景!!!比如高频执行某个任务最好还是使用重写QThread::run()的方式。

加载全部内容

相关教程
猜你喜欢
用户评论