< MyThread.h>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QThread *parent = 0);
virtual ~MyThread();
void startThread();
void stopThread();
protected:
virtual void run();
signals:
void started();
void finished();
protected:
bool m_bRun;
}
< MyThread.cpp>
MyThread::MyThread(QThread *parent) : QThread(parent);
{
m_bRun = false;
}
MyThread::~MyThread()
{
}
void MyThread::startThread()
{
m_bRun = true;
QThread::start();
}
void MyThread::stopThread()
{
m_bRun = false;
QThread::wait();
}
void MyThread::run()
{
emit started();
while (m_bRun)
{
...
}
emit finished();
}