您的位置:首页 > 编程语言 > Qt开发

Qt QThread多线程模版

2017-01-23 10:07 309 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/d821040387/article/details/54691571

WorkerThread.h头文件

#ifndef WORKERTHREAD_H
#define WORKERTHREAD_H
#include <QThread>
#include <QDebug>
class WorkerThread : public QThread
{
Q_OBJECT
public:
WorkerThread(QObject* parent = NULL);
~WorkerThread();
void startThread();
void quitThread();
void run();
protected:
bool bQuitThread;
};

#endif // THREAD_H
WorkerThread.cpp源文件

#include "WorkerThread.h"

WorkerThread::WorkerThread(QObject* parent):QThread(parent),bQuitThread(false)
{

}
WorkerThread::~WorkerThread()
{
quitThread();
}
void WorkerThread::startThread()
{
if(!this->isRunning()){
bQuitThread = false;
this->start();
}
}
void WorkerThread::quitThread()
{
if(isRunning()){
bQuitThread = true;
this->wait();
}
}
void WorkerThread::run()
{
while (!bQuitThread) {
qDebug() << "ThreadId =" <<currentThreadId();
msleep(50);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: