我的信号和插槽有问题,我想使用一个额外线程的后台工作程序。它假设发送一个带有几个双精度值的信号,然后应该在主 gui 中进行更新。那么代码编译并且线程也启动但是gui没有更新值。
首先是gui插槽:
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));
}
工人代码:
void dworker::run()
{
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);
for (int i = 0; i<100; i++)
{
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);
emit set_pmt();
QThread::msleep(1000);
qDebug() << "Test vom Thread " << i;
}
}
和连接语句:
connect(workerthread,SIGNAL(set_values(double,double,double,double,double,double)),
this,SLOT(slot_set_values(double,double,double,double,double,double)));
connect(workerthread,SIGNAL(set_pmt()),this,SLOT(slot_set_pmt()));