问题

下述代码,无论如何都进不到槽函数MonoCalibrate::calibrate里面

1
2
3
4
5
6
7
QThread *caliThread = new QThread(this);
MonoCalibrate *monocali = new MonoCalibrate(m_res_ckbd);
monocali->moveToThread(caliThread);
caliThread->start();

emit monocali->startCalibration();
connect(monocali,&MonoCalibrate::startCalibration,monocali,&MonoCalibrate::calibrate);

解决

需要先连接(connect)再发射信号(emit):

在连接信号和槽函数之前就发出了信号,将导致信号连接无效,因为在连接之前,没有接收槽函数的对象。

为了解决这个问题,可以将信号发出的代码移到连接之后。这样,当信号被发出时,槽函数已经连接并准备好执行

1
2
3
4
5
6
7
8
9
10
11
qDebug() << QThread::currentThread();

QThread *caliThread = new QThread(this);
MonoCalibrate *monocali = new MonoCalibrate(m_res_ckbd);
monocali->moveToThread(caliThread);
caliThread->start();

connect(monocali, &MonoCalibrate::startCalibration, monocali, &MonoCalibrate::calibrate);

// 在连接之后发出信号
emit monocali->startCalibration();