一般我们使用多线程为两种方式:
1. 要使用的类,继承QThread
要注意这种情况,函数需要写在run里面
#include <QThread>
class Worker : public QThread
{
public:
Worker();
void run(){printFunc();};//只有在run函数里面,才是printFunc多线程
void printFunc();
};
2. 要使用的类worker->moveToThread(workerThread)
要使用connect连接触发
// 创建 Worker 实例
Worker *worker = new Worker();
QThread *workerThread = new QThread();
worker->moveToThread(workerThread);
// 要使用connect,否则无法启动
connect(workerThread, &QThread::started, worker, &Worker::doWork);
workerThread->start();