文章目录
1. 引言
你会用QThread吗?有几种使用方式?这几种使用方式都在何种场景下使用?有什么需要注意的地方吗?
2. QThread 文档
上来先看 Qt 帮助文档。QThread Class 文档,详细描述的第一句话:
The QThread class provides a platform-independent way to manage threads
注意看倒数第二个单词,QThread 不等于线程,QThread 是负责管理线程的。
接下来看文档,我们清楚的知道QThread的两种使用方式。
方式一:子类化QThread,并重新实现 run() 函数
方式二:定义工作对象继承自 QObject,然后把这个工作对象move到QThread的一个对象中。
3. QThread::run 和 QObject::connect
先看下帮助文档上怎么描述这个 QThread::run 函数的:
The starting point for the thread. After calling start(), the newly created thread calls this function. The default implementation simply calls exec().
You can reimplement this function to facilitate advanced thread management. Returning from this method will end the execution of the thread.
这段英文描述的很