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);
}
}