我的Qt Thread问题和它的信号。 我正在创建一个名为dworker的QObject并将其移动到QThread,然后单击我的开始按钮启动该线程。 它基本上只是用一些数字更新GUI,并且当我按下停止按钮时应该停止。好吧它停止但是“qDebug()<<”thread stops =“<< stop;”在dworker没有被调用,我不能再启动它。信号是triggert但不执行槽方法。
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
namespace GLOBAL
{
Settings mSettings;
Data mData;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dthread = new QThread(this);
dworker = new Dworker();
dworker->moveToThread(dthread);
connect(dworker, SIGNAL(set_values(double,double,double,double,double,double)),
this, SLOT(slot_set_values(double,double,double,double,double,double)));
connect(ui->startButton, SIGNAL(clicked()), dworker, SLOT(slot_process()));
connect(ui->stopButton, SIGNAL(clicked()), dworker, SLOT(slot_end_process()));
dthread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slot_set_values(double ptm_temp, double ptm_hv, double heat_temp, double nomtemp, double current, double voltage)
{
ui->pmtValueLabel->setText(QString::number(ptm_temp));
ui->hvValueLabel->setText(QString::number(ptm_hv));
ui->heatValueLabel->setText(QString::number(heat_temp));
ui->nomValueLabel->setText(QString::number(nomtemp));
ui->currenValueLabel->setText(QString::number(current));
ui->vValueLabel->setText(QString::number(voltage));
//qDebug() <<"set_values SLOT " <<ptm_temp<<" "<<ptm_hv<<" "<<heat_temp<<" "<<nomtemp<<" "<<current<<" "<<voltage;
}
void MainWindow::on_startButton_clicked()
{
}
void MainWindow::on_stopButton_clicked()
{
dworker->stop = true;
qDebug() << "send stop";
}
dworker.cpp:
#include "dworker.h"
using namespace GLOBAL;
QMutex mutex;
Dworker::Dworker(QObject *parent) :
QObject(parent)
{
}
void Dworker::slot_process()
{
stop = false;
while (true)
{
mutex.lock();
if(stop) break;
mutex.unlock();
qsrand(QDateTime::currentDateTime().toTime_t());
mData.set_pmt_temp(qrand()%100);
mData.set_pmt_hv(qrand()%100);
mData.set_heat_opt_temp(qrand()%100);
mData.set_heat_nominal_temp(qrand()%100);
double pmt_tmp = mData.get_pmt_temp();
double hv = mData.get_pmt_hv();
double heat_temp = mData.get_heat_opt_temp();
double heat_nom = mData.get_heat_nominal_temp();
emit set_values(pmt_tmp,hv,heat_temp,heat_nom,0,0);
QThread::msleep(1000);
qDebug() <<"Thread SIGNAL " <<pmt_tmp<<" "<<hv<<" "<<heat_temp<<" "<<heat_nom;
}
}
void Dworker::slot_end_process()
{
mutex.lock();
stop = true;
mutex.unlock();
qDebug() << "thread stopping = " << stop;
}
答案 0 :(得分:0)
很多问题!
您正在使用连接Qt::AutoConnection
中的默认值。这意味着如果通过目标线程的事件循环在踏板之间传递信号,请参阅doc。这就是为什么你不能停止你的功能,你的事件循环永远不会在slot_process()
运行时获得控制权。要修复它,请更改连接(使用Qt::DirectConnection
):
connect(ui->stopButton, SIGNAL(clicked()),
dworker, SLOT(slot_end_process())
Qt::DirectConnection);
另一个问题:你在slot_process()
错误地锁定了互斥锁,请注意,break会跳过解锁状态,互斥锁将永久锁定,修复如下:
{
QMutexLocker locker(&mutex);
if(stop) break;
} // braces are important