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()

想问下这个是怎么回事啊?
...全文
339 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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
  • 打赏
  • 举报
回复
信号和槽也是基于事件循环的,如果你想给主线程给子线程发送信号,那子线程就要开启事件循环,否则是收不到的
Qt视频播放器 Qt对音视频的播放和控制、相机拍摄、收音机等多媒体应用提供了强大的支持。 Qt5使用了全新的Qt Multimedia模块来实现多媒体应用,Qt4中用来实现多媒体功能的Phonon模块已经被移除。 新的Qt Multimedia模块提供了丰富的接口,可以轻松地使用平台的多媒体功能。例如进行媒体播放、使用相机和收音机等。 QMediaPlayer简介 QMediaPlayer播放视频要在界面上显示出来,还需要其他类进行辅助,比如QVideoWidget类。:编解码 QVideoWidget继承自QWidget,所有它可以作为一个普通窗口部件进行显示,也可以嵌入到其他窗口。:播放窗口 将QVideoWidget指定为QMediaPlayer的视频输出窗口后,就可以显示播放的视频画面。 二者组合,可以播放视频: player = new QMediaPlayer; videoWidget = new QVideoWidget; player->setVideoOutput(videoWidget); videoWidget->show(); player->play(); QVideoWidget简介 QvideoWidget是一个用来展示视频的类,需要先定义一个QMediaPlayer对象,然后将QMediaPlayer的VideoOutput设置为QVideoWidget对象即可 QVideoWidget属性 名称 类型 说明 aspectRatioMode Qt::AspectRatioMode 显示比率 brightness int 亮度 contrast int 对比度 fullScreen bool 是否全屏 hue int 色调 mediaObject QMediaObject *const 视频媒体对象 saturation int 饱和度 error: C1083: 问题:qt creator报错 error: C1083: 无法打开包括文件:“QMediaPlayer” 解决:QT += multimedia multimediawidgets 如何获取duration QT关于QMediaplayer 的duration()获取的音视频时间长度为0的问题。   在QT中,使用QMediaplayer类可以很方便地实现视频的播放,而在QMediaplayer类中有个duration函数可以直接获取所打开视频的总时间长度。 但使用后你会发现duration()返回的居然是个0。   官方解释: The value may change across the life time of the QMediaPlayer object and may not be available when initial playback begins, connect to the durationChanged() signal to receive status notifications. 在初始回放开始时可能不可用,请连接durationChanged()信号以接收状态通知。 即我们只需要写个槽函数,在槽函数里面调用duration()就可以接收到正确的时间 例: //第一步:连接槽函数,信号为QMediaPlayer自带的durationChanged,槽就是自己定义的getduration,注意参数类型要一致 QObject::connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(getduration(qint64))); //第二步:写槽函数,mediaplay为类名,不同类需要修改这个类名,playtime为总时长 void Widget::getduration(qint64 playtime) { // 在这里获取duration(...) playtime = player->duration(); } 经过以上两步就可以获得正确的时间啦。 以下再附上把获得的时间转化为时分秒的函数: QString Widget::convertMillisecToString(qint64 millisec) { int h,m,s; millisec /= 1000; //获得的时间是以毫秒为单位的 h = (int)millisec / 3600; m = ((int)millisec - h*3600)/60; s = (int)millisec - h*3600 - m*60; //把int型转化为string类型 QString strFormat = QString("%1:%2:%3") .arg(h,2,10,QChar('0')) .arg(m,2,10,QChar('0')) .arg(s,2,10,QChar('0')); return strFormat; } 双击全屏显示

16,815

社区成员

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

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