Reputation: 99
I have an issue with my signals and slots, I want to use a backgroundworker which is an extra thread. Its suppose to send a signal with a few double values which should then be updated in the main gui. Well the code compiles and the thread starts also but the gui is not updating the values.
first the gui slot:
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));
}
the worker code:
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;
}
}
and the connect statements:
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()));
Upvotes: 1
Views: 153
Reputation: 3493
If object that sends signal and receiver object are in a different threads, you should connect it with Qt::QueuedConnection
(docs here)
So, change you connects to this:
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);
Additionally, you can try to check via qDebug, what are you receiving in the slot:
qDebug()<<"my slot is called,"<<ptm_temp<<" "<<ptm_hv<<" "<<heat_temp<<" "<<nomtemp<<" "<<current<<" "<<voltage;
Also, it is imperative to have signal-slots, that in header of your derived class was Q_OBJECT
macro
Upvotes: 1