mimin February 2016

How to emit a signal with array parameter to another Thread

I am new to Qt and I want to emit a signal from a QThread to main Thread with a few parameter. It doesn't work. What am I doing wrong?

//reader.h
signals:
        void updateData(quint16 fw, QString serial, quint16 *calibration);
//reader.cpp
...
{
    quint16 calibration[8];
    quint16 fw;
    QString serialNumber;
    readData(&fw, &serialNumber, calibration);
    // Declaration: void Reader::readData(quint16 *fw, QString *serial, quint16 *calibration) 
    emit updateData(fw, serialNumber, calibration); // In this function all parameters are assigned correctly
}
//mainwindow.h
 public slots:       
            void updateData(quint16 fw, QString serialNumber, quint16 *calibration);
//mainwindow.cpp
void MainWindow::updateData(quint16 fw, QString serialNumber, quint16 *calibration)
{   
    ui->lineEditFW->setText(QString::number(fw));     // correct
    ui->lineEditSerialNumber->setText(serialNumber);  // correct
    ui->spinBoxReg1->setValue(calibration[0]);        // incorrect!
    ui->spinBoxReg2->setValue(calibration[1]);        // ..array calibration
    ui->spinBoxReg3->setValue(calibration[2]);        // ..is filled with
    ui->spinBoxReg4->setValue(calibration[3]);        // ..random data!
    ui->spinBoxReg5->setValue(calibration[4]);
    ui->spinBoxReg6->setValue(calibration[5]);
    ui->spinBoxReg7->setValue(calibration[6]);
    ui->spinBoxReg8->setValue(calibration[7]);
}
//...
init()
{
   // other connection of thread and reader object
   //...
   connect(reader, SIGNAL(updateData(quint16, QString, quint16 *)), this, SLOT(updateData(quint16, QString, quint16 *)));
   //...
   reader->moveToThread(readerThread);
   readerThread->start();
}

I also tried it with array instead of pointer. It didn't work as well.

Thanks.

Answers


Stuart Fisher February 2016

calibration[8] is on the stack, so it is destroyed when your reader function returns. You need to make it persistent (allocate it on the heap) so that it still exists when the signal is delivered.

Post Status

Asked in February 2016
Viewed 2,548 times
Voted 7
Answered 1 times

Search




Leave an answer