参考文章:C++/Qt 多线程
一、Concurrent::run
多线程
1、设置打开
打开QT Project Setting
点击Qt Modules
把Concurrent打钩
2、代码
输入头文件
#include <QtConcurrent>
使用
//QFuture<T> QtConcurrent::run(Function function, ...)
QString str = "test";
QFuture<void> fun_res1 = QtConcurrent::run(fun1,str)
//QFuture<T> QtConcurrent::run(QThreadPool *pool, Function function, ...)
QFuture<void> fun_res2 = QtConcurrent::run(this, &MainFrameUi::fun2, str)
二、 moveToThread
实现多线程
1、简介
此类多线程由QObject
类提供
官方描述
moveToThread (QThread * targetThread)
//Changes the thread affinity for this object and its children. The object cannot be moved if it has a parent. Event processing will continue in the targetThread.
更改此对象及其子对象的线程亲和类型。如果对象具有父对象,则不能移动该对象。事件处理将在 targetThread 中继续。
2、建立Worker
类
此类是用于定义线程需要做的费时工作,此类重点在于定义
doWork()
槽函数,其执行是就是子线程执行。
Worker.h
文件
#pragma once