如何向另一个线程发出带有数组参数的信号
How to emit a signal with array parameter to another Thread
我是 Qt 的新手,我想用一些参数从 QThread 向主线程发出信号。它不起作用。我做错了什么?
//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();
}
我也试过用数组代替指针。它也没有用。
谢谢。
校准[8]在堆栈上,所以当你的reader函数returns时它被销毁了。你需要让它持久化(在堆上分配它),这样当信号传递时它仍然存在。
我是 Qt 的新手,我想用一些参数从 QThread 向主线程发出信号。它不起作用。我做错了什么?
//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();
}
我也试过用数组代替指针。它也没有用。
谢谢。
校准[8]在堆栈上,所以当你的reader函数returns时它被销毁了。你需要让它持久化(在堆上分配它),这样当信号传递时它仍然存在。