class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
void WorkRun();
void setFlag(bool flag = true);
signals:
void signal_back();//处理结果返回信号
private:
bool isStop;
};
#include "controller.h"
#include <QDebug>
Worker::Worker(QObject *parent) : QObject(parent)
{
isStop = false;
}
void Worker::WorkRun() //线程处理函数:具体处理的事情
{
while(!isStop)
{
QThread::sleep(1);
emit signal_back(); //发送返回信号
qDebug() << "the child thread number:" << QThread::currentThread();
}
}
void Worker::setFlag(bool flag)
{
isStop = flag;
}
subthread = new QThread(this);
m_MyThread = new Worker();
m_MyThread->moveToThread(subthread);
connect(this, &Widget::StartThread, m_MyThread, &Worker::WorkRun);
connect(m_MyThread, &Worker::signal_back, this, &Widget::slot_handle_finish);
subthread->start();
emit StartThread();