QT 发送signal但是无法到达SLOT函数,修改下程序就可以,望大牛解答为什么?

童心童颜 2015-07-30 08:32:35
main函数部分代码如下:
Message_Handler * pMsgHandler = new Message_Handler();
QThread *pMsgHandlerThread = new QThread;
pMsgHandler->moveToThread(pMsgHandlerThread);
QObject::connect(pMsgHandlerThread, SIGNAL(started()), pMsgHandler, SLOT(handleMessage()));
pMsgHandlerThread->start(QThread::NormalPriority);

QObject::connect(pServerStatusGui, SIGNAL(signalSendClientSetupInitNetMsg()),pMsgHandler, SLOT(sendClientSetupInitNetMsg()),Qt::AutoConnection);


如果我发送signalSendClientSetupInitNetMsg(),那么QT无法找到slot sendClientSetupInitNetMsg()


如果程序修改成
Message_Handler * pMsgHandler = new Message_Handler();
//QThread *pMsgHandlerThread = new QThread;
// pMsgHandler->moveToThread(pMsgHandlerThread);
// QObject::connect(pMsgHandlerThread, SIGNAL(started()), pMsgHandler, SLOT(handleMessage()));
// pMsgHandlerThread->start(QThread::NormalPriority);

QObject::connect(pServerStatusGui, SIGNAL(signalSendClientSetupInitNetMsg()),pMsgHandler, SLOT(sendClientSetupInitNetMsg()),Qt::AutoConnection);

如果我发送signalSendClientSetupInitNetMsg(),那么QT能够找到slot sendClientSetupInitNetMsg()

想问下这个是怎么回事啊?
...全文
333 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaozhiyong110 2015-07-30
  • 打赏
  • 举报
回复
Qt::AutoConnection (default) If the signal is emitted from a different thread than the receiving object, the signal is queued, behaving as Qt::QueuedConnection. Otherwise, the slot is invoked directly, behaving as Qt::DirectConnection. The type of connection is determined when the signal is emitted. Qt::QueuedConnection The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
yaozhiyong110 2015-07-30
  • 打赏
  • 举报
回复
前一个因为你 pMsgHandler->moveToThread(pMsgHandlerThread); 对象属性移到子线程了 所以槽函数在子线程执行 因为你子线程没有event loop 所以没接收信号... ps: Qt 助手里去参考 Qt::ConnectionType
donwmufromdying 2015-07-30
  • 打赏
  • 举报
回复
子线程的run里放个exec();
童心童颜 2015-07-30
  • 打赏
  • 举报
回复
了解了,多谢,线程的run函数被重写了,也是个无限循环,看来只能想想别的方法了。
Creator_莫言 2015-07-30
  • 打赏
  • 举报
回复
信号和槽也是基于事件循环的,如果你想给主线程给子线程发送信号,那子线程就要开启事件循环,否则是收不到的

16,211

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧