QT创建线程

张开发
2026/5/19 9:06:23 15 分钟阅读
QT创建线程
1、使用Worker moveToThread的方式将任务移动到线程实例对象中。1创建循环子线程#ifndef WORKER_H #define WORKER_H #include QObject #include chrono #include QThread #include QDebug class Worker:public QObject { Q_OBJECT public: explicit Worker(QObject* parent nullptr); public slots: void doWork(); signals: void workFinished(); }; #endif #include Worker.h Worker::Worker(QObject* parent):QObject(parent) { } void Worker::doWork() { int a 0; qDebug() 子线程ID QThread::currentThreadId(); while(1) { std::this_thread::sleep_for(std::chrono::seconds(1)); qDebug() a; if(a 10) break; } //任务完成退出 emit workFinished(); } #include QCoreApplication #include QThread #include QTimer #include QDebug #include Worker.h int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //获取主线程id qDebug() 主线程ID QThread::currentThreadId(); //创建线程实例 QThread* taskThread new QThread(); //创建任务对象 Worker* worker new Worker(); //将任务对象worker移动到线程实例中 worker-moveToThread(taskThread); QObject::connect(taskThread,QThread::started,worker,Worker::doWork);//任务开始 QObject::connect(worker,Worker::workFinished,taskThread,QThread::quit);//退出线程 QObject::connect(taskThread,QThread::finished,taskThread,QThread::deleteLater);//清理线程对象 QObject::connect(taskThread,QThread::finished,worker,QThread::deleteLater);//清理任务对象 //启动子线程 taskThread-start(); return a.exec(); }2创建非循环子线程在子线程中使用定时器。#ifndef WORKER_H #define WORKER_H #include QObject #include chrono #include QThread #include QDebug #include QTimer class Worker:public QObject { Q_OBJECT public: explicit Worker(QObject* parent nullptr); public slots: void doLoopWork(); void startTimer();//在子线程中启动定时器 private slots: void doTimerWork(); signals: void workFinished(); private: QTimer* timer nullptr; }; #endif #include Worker.h Worker::Worker(QObject* parent):QObject(parent) { } void Worker::doLoopWork() { int a 0; qDebug() 子线程ID QThread::currentThreadId(); while(1) { std::this_thread::sleep_for(std::chrono::seconds(1)); qDebug() a; if(a 10) break; } //任务完成退出 emit workFinished(); } void Worker::startTimer() { qDebug() 启动定时器线程ID QThread::currentThreadId(); timer new QTimer(this); timer-setInterval(1000); QObject::connect(timer,QTimer::timeout,this,Worker::doTimerWork); timer-start(); } void Worker::doTimerWork() { static int a 0; qDebug() a; if(a 10) { if(timer) { timer-stop(); } } } #include QCoreApplication #include QThread #include QTimer #include QDebug #include Worker.h int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //获取主线程id qDebug() 主线程ID QThread::currentThreadId(); //创建线程实例 QThread* taskThread new QThread(); //创建任务对象 Worker* worker new Worker(); //将任务对象worker移动到线程实例中 worker-moveToThread(taskThread); QObject::connect(taskThread,QThread::started,worker,Worker::startTimer);//任务开始 QObject::connect(worker,Worker::workFinished,taskThread,QThread::quit);//退出线程 QObject::connect(taskThread,QThread::finished,taskThread,QThread::deleteLater);//清理线程对象 QObject::connect(taskThread,QThread::finished,worker,QThread::deleteLater);//清理任务对象 //启动子线程 taskThread-start(); return a.exec(); }

更多文章