1

我的信号和插槽有问题,我想使用一个额外线程的后台工作程序。它假设发送一个带有几个双精度值的信号,然后应该在主 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())); 
4

1 回答 1

1

如果发送信号的对象和接收对象位于不同的线程中,则应将其连接(此处Qt::QueuedConnection的文档)

所以,改变你连接到这个:

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

于 2013-08-07T23:59:38.060 回答