Qt提供了一个管理线程的类:QThread。
一、公共槽函数:
1)start():开始线程的执行,内部调用run()函数,run()又调用exec()。
2)quit():告诉线程的事件循环停止运行,并返回0(成功),等价于调用exit(0);
3)terminiate():「不推荐使用该函数」终止线程的执行。线程可能不会立即终止,取决于操作系统的调度。
二、信号:
1)started():在start()之后,在run()之前发射。
2)funished():在线程将要停止执行时发射。发射时,线程已经停止事件循环。不再执行新的事件,但是deferred deletion事件除外。可以把该信号连接到本线程中的对象的deleteLater()槽中。
三、公共函数:
wait():等待线程停止执行,通常和quit()配合使用
- void msleep(unsigned long msecs) [static]
-
- 强制当前线程睡眠msecs毫秒。
-
- void sleep(unsigned long secs) [static]
-
- 强制当前线程睡眠secs秒。
-
- void usleep(unsigned long usecs) [static]
-
- 强制当前线程睡眠usecs微秒。
-
- bool wait(unsigned long time = ULONG_MAX)
-
- 线程将会被阻塞,等待time毫秒。和sleep不同的是,如果线程退出,wait会返回
first.创建一个 QThread 和 QWorker (继承自 QObject) 类对象;
second. 使用 moveToThread 函数移动到 thread 中运行;
third. 通过 thread 类 start 信号和 worker 槽函数绑定;
four. 调用 thread 类 start 信号 ,运行;
- QThread* thread = new QThread();
- WorkObject* worker = new WorkObject();
- worker->moveToThread(thread);
- QObject::connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
-
- thread->start();
实现代码:
workobject.h
- #ifndef WORKOBJECT_H
- #define WORKOBJECT_H
-
-
- #include
- #include
- #include
- class WorkObject : public QObject
- {
-
- Q_OBJECT
- public:
- explicit WorkObject(QObject *parent = nullptr);
-
- signals:
-
- public slots:
- void doWork();
- };
-
- #endif // WORKOBJECT_H
workobject.cpp
- #include "workobject.h"
-
- WorkObject::WorkObject(QObject *parent) : QObject(parent)
- {
-
- }
- void WorkObject::doWork()
- {
-
-
- qDebug() << "doWork thread id:" << QThread::currentThreadId();
- }
main.cpp
- #include
- #include "workobject.h"
-
-
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
-
-
- qDebug() << "main thread id:" << QThread::currentThreadId();
-
- QThread* thread = new QThread();
- WorkObject* worker = new WorkObject();
- worker->moveToThread(thread);
-
- QObject::connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
- QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
-
- thread->start();
-
- return a.exec();
- }
运行结果:
可以看到,doWork 执行在子线程;
继承自 QThread 类,覆写 run 函数。此实现方法只有 run 函数内的代码是运行在子线程内
mythread.h
- #ifndef MYTHREAD_H
- #define MYTHREAD_H
-
-
-
- #include
- #include
-
- class MyThread : public QThread
- {
- Q_OBJECT
-
- public:
- MyThread(QObject* parent = nullptr);
- ~MyThread();
-
- protected:
- void run() override;
-
- public:
- void stop();
-
- private:
- bool exitflag;
- };
- #endif // MYTHREAD_H
mythread.cpp
- #include "mythread.h"
-
- MyThread::MyThread(QObject* parent) : QThread(parent)
- {
- }
-
- MyThread::~MyThread()
- {
- }
-
- void MyThread::run()
- {
- exitflag = true;
- while(exitflag)
- {
- qDebug() << "MyThread thread id:" << QThread::currentThreadId();
- sleep(1);
- }
- }
-
- void MyThread::stop()
- {
- exitflag = false;
- if(isRunning())
- {
- exit();
- wait();
- }
- }
main.cpp
- #include
- #include "workobject.h"
-
- #include "mythread.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
-
-
- qDebug() << "main thread id:" << QThread::currentThreadId();
-
-
- MyThread* myThread = new MyThread();
- myThread->start();
-
- return a.exec();
- }
输出:
- main thread id: 0x700
- MyThread thread id: 0x4ffc
- MyThread thread id: 0x4ffc
- MyThread thread id: 0x4ffc