我的 Qt 线程和它的信号有问题。我正在创建一个名为 dworker 的 QObject 并将其移动到 QThread ,然后单击我的开始按钮启动线程。它基本上只是用一些数字更新 GUI,当我按下停止按钮时应该停止。好吧,它停止了,但是“qDebug()<<”线程停止=“<<停止;” in dworker 没有被调用,我无法再次启动它。信号已触发,但插槽方法未执行。
主窗口.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;
}