WorkerThread* pWorkerThread = new WorkerThread;
pWorkerThread->moveToThread(&m_thread);
connect(this, &Dialog::sigSetCOM, pWorkerThread, &WorkerThread::slotSetCOM, Qt::QueuedConnection);
connect(this, &Dialog::sigSerialOpen, pWorkerThread, &WorkerThread::slotSerialOpen, Qt::QueuedConnection);
connect(this, &Dialog::sigSetFilePath, pWorkerThread, &WorkerThread::slotSetFilePath, Qt::QueuedConnection);
connect(pWorkerThread, &WorkerThread::sigSetProgressValue, this ,&Dialog::slotSetProgressValue, Qt::QueuedConnection);
connect(pWorkerThread, &WorkerThread::resultReady, this ,&Dialog::handleResults, Qt::QueuedConnection);
connect(pWorkerThread, &WorkerThread::sigBtOpenStateChange, this ,&Dialog::slotBtOpenStateChanged, Qt::QueuedConnection);
connect(pWorkerThread, &WorkerThread::sigResetState, this ,&Dialog::slotResetState, Qt::QueuedConnection);
connect(this, &Dialog::sigSend, pWorkerThread, &WorkerThread::slotSend);
connect(&m_thread, &QThread::finished, pWorkerThread, &QObject::deleteLater);
m_thread.start();
// 设置参数
//emit ui->serialPortName->currentIndexChanged(ui->serialPortName->currentText());
emit sigSetCOM(m_param);
上面的槽函数Dialog::slotBtOpenStateChanged(倒数第四个connect) 只连接了一次如下: connect(pWorkerThread, &WorkerThread::sigBtOpenStateChange, this ,&Dialog::slotBtOpenStateChanged, Qt::QueuedConnection);
按理说是pWorkerThread线程中信号WorkerThread::sigBtOpenStateChange触发后才会调用槽函数Dialog::slotBtOpenStateChanged,但是我的代码中只是在pWorkerThread触发了一次信号,而实际槽函数Dialog::slotBtOpenStateChanged却被调用了两次。一次与pWorkerThread有关。一次与Dialog有关,因为都有一个bool参数,Dialog触发的带的值不是我想要的。这与Dialog有关的多一次调用是为什么???