我的信号和插槽有问题,我想使用背景工作,这是一个额外的线程。它假设发送一个带有几个double值的信号,然后在主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()));
答案 0 :(得分:1)
如果发送信号和接收者对象的对象位于不同的线程中,则应将其与Qt::QueuedConnection
(文档here)
所以,改变你连接到这个:
connect(workerthread,SIGNAL(set_values(double,double,double,double,double,double)), this,SLOT(slot_set_values(double,double,double,double,double,double)),Qt::QueuedConnection);
connect(workerthread,SIGNAL(set_pmt()),this,SLOT(slot_set_pmt()),Qt::QueuedConnection);
此外,您可以尝试通过qDebug进行检查,您在插槽中收到了什么:
qDebug()<<"my slot is called,"<<ptm_temp<<" "<<ptm_hv<<" "<<heat_temp<<" "<<nomtemp<<" "<<current<<" "<<voltage;
此外,必须有信号槽,在派生类的标题中是Q_OBJECT
宏