下面是主界面的代碼:
new_qingsha_by_qt::new_qingsha_by_qt(QWidget *parent)
: QMainWindow(parent)
{
ui->setupUi(this);
QPixmap pixmap("./pic/logo.png");
ui->icon->setPixmap(pixmap);
// 引數1 信號發送者(指標),引數2 發送的信號(信號地址),引數3 信號接受者(指標),引數4信號處理函式(函式地址)
//connect(ui->closeBtn, &QPushButton::clicked, this, &new_qingsha_by_qt::close);
connect(ui->closeBtn, SIGNAL(clicked()), this, SLOT(on_close_clicked()));
connect(ui->startBtn, SIGNAL(clicked()), this, SLOT(on_start_clicked()));
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_clear_clicked()));
connect(ui->pauseBtn, SIGNAL(clicked()), this, SLOT(on_pause_clicked()));
//執行緒的初始化
m_thread = new MyThread;
m_childThread = new QThread; // 子執行緒本身不負責處理
m_thread->moveToThread(m_childThread); //將實體移動到新的執行緒,實作多執行緒運行
connect(this, SIGNAL(startThread()), m_thread, SLOT(startHandle()));
connect(this, SIGNAL(stopThread()), m_thread, SLOT(cancelHandle()));
connect(m_thread, SIGNAL(callFather(void)), this, SLOT(closeThread()));
m_childThread->start();//啟動子執行緒
}
new_qingsha_by_qt::~new_qingsha_by_qt()
{
delete ui;
}
void new_qingsha_by_qt::on_close_clicked()
{
QApplication::exit();
}
void new_qingsha_by_qt::on_start_clicked()
{
qDebug() << "main Thread:" << QThread::currentThread();
qDebug() << "thread start";
emit startThread();
//ui->startBtn->setEnabled(false);
}
void new_qingsha_by_qt::closeThread()
{
qDebug() << "shutdown";
}
void new_qingsha_by_qt::on_clear_clicked()
{
ui->bad_pic->clear();
ui->textEdit->clear();
ui->result->clear();
}
void new_qingsha_by_qt::on_pause_clicked()
{
qDebug() << "thread stop";
emit stopThread(); //這個沒有觸發
//emit startThread(0);
}
下面是具體的多執行緒槽函式的實作:
MyThread::MyThread(QObject* parent) :QObject(parent)
{
m_bRun = false;
}
MyThread::~MyThread()
{
}
void MyThread::startHandle()
{
qDebug() << "sub thread:" << QThread::currentThread();
for(;;)
{
QThread::sleep(10);
qDebug() << "isRuning";
{
//QMutexLocker locker(&m_Mutex);
if (m_bRun)
{
emit callFather();
break;
}
}
}
}
void MyThread::cancelHandle()
{
qDebug() << "sub thread" << QThread::currentThread();
qDebug() << "cancelHandle thread";
m_bRun = true;
}
主要想通過m_bRun標志位的改變來改變執行緒的運行,但是點擊pause按鈕之后,發現槽函式并沒有觸發,執行緒一直在開啟的狀態,請問這個該怎么處理?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260754.html
標籤:其他技術討論專區
