目录 1. 概述 2. moveThread 示例 3. QThread 示例 4. 总结 1. 概述 QThread 有两种使用方式 QObject::moveToThread() 派生 QThread 的子类类 2. moveThread 示例 步骤概述: 定义一个 QObject 派生类,在派生类中
目录
- 1. 概述
- 2. moveThread 示例
- 3. QThread 示例
- 4. 总结
1. 概述
QThread 有两种使用方式
QObject::moveToThread()- 派生
QThread的子类类
2. moveThread 示例
步骤概述:
- 定义一个
QObject派生类,在派生类中定义一个槽函数,此函数是用于执行具体的工作 - 在要使用线程的类中,新建
QThread和QObject派生类对象,并使用moveThread()将派生类的处理交由QThread - 将触发线程工作的信号与派生类的槽函数进行连接
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 示例
方法概述:
- 定义一个
QThread的派生类,并重载run()函数,在run()函数中写入具体的线程代码 - 通过
start()启动线程
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: 0x6544thread run finish!
current thread ID(in slot function): 0x6508
4. 总结
moveToThread
此方式,要求把需要进行的工作全部封装在一个类中,将每一个任务定义为一个槽函数,并与之对应的信号进行关联,最后调用moveToThread将此类交QThread对象。QThread调用start()进行启动,之后每个任务由相应的信号进行触发然后执行。
QThread
此方式是要求基于QThread进行派生,对派生类进行run()函数的override。之后调用start()后,就会运行run()函数。但是在派生类中定义的槽函数,不会由派生类自身所执行,而是由该线程的拥有者执行。
QThread只有run函数是在新线程里执行,其他所有函数都在QThread生成的线程里执行
官方是比较推荐使用moveToThread的方式,不过也看各自的使用场景!!!比如高频执行某个任务最好还是使用重写QThread::run()的方式。
到此这篇关于QT线程QThread的使用介绍的文章就介绍到这了,更多相关QT线程QThread内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
