[英]Qt Signal calling from second thread works -> no effect on connected slot
我有一个Qt应用程序。 我的类ViewSimulation
有一个static-Methode loggingHandler
,它调用(发出)类信号logging
。 方法loggingHandler
将作为函数指针传递给c文件publisher.c
。 publisher.c
在单独的线程中调用其功能指针(*logger)
。
在调试中,我看到publisher.c
调用ViewSimulation::loggingHandler
并发出logging
的信号,但是连接的插槽没有反应。 但是,如果在主线程中logging
了调用/发出消息,则&ViewGooseList::logging)
的插槽会做出反应。
为什么插槽没有通过从publisher.c
的另一个c线程 “调用”来做出反应?
ViewSimulation.cpp / h
class ViewSimulation : public QGroupBox
{
...
signals:
void logging(int id, uint64_t timestamp);
private:
static void loggingHandler(int id, uint64_t timestamp);
ViewSimulation* ViewSimulation::m_current;
...
void ViewSimulation::loggingHandler(int id, uint64_t timestamp)
{
emit ViewSimulation::m_current->logging(id, timestamp);
}
...
connect(m_gooseSimulation, &ViewSimulation::logging, m_gooseList, &ViewGooseList::logging);
setLogging(loggingHandler);
Publisher.c / h
/*Header*/
void setLogging(void (*logging)(int, uint64_t));
static void (*logger)(int gooseId, uint64_t timestamp);
/*C-File*/
void setLogging(void (*logging)(int, uint64_t))
{
logger = logging;
}
...
/*This methode will be called from main-thread **AND** from second thread */
logger(gooseMessage->id, gooseMessage->lastTimeStamp);
...
对于跨线程的信号/插槽连接,应创建排队的连接:
connect(m_gooseSimulation, &ViewSimulation::logging, m_gooseList,
&ViewGooseList::logging, Qt::QueuedConnection);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.