QT中解决QSocketNotifier: socket notifiers cannot be enabled from another thread错误
来源:互联网 发布:卖家怎么在淘宝客推广 编辑:程序博客网 时间:2024/05/01 13:57
class UDPReceive : public QThread{ Q_OBJECTpublic: UDPReceive(); UDPReceive(QUdpSocket *s);protected: virtual void run();private: QUdpSocket *udp_socket; };UDPReceive::UDPReceive(){}UDPReceive::UDPReceive(QUdpSocket *s){ this->udp_socket = s;}void UDPReceive::run(){ while(1) { if (udp_socket->hasPendingDatagrams()) { if (udp_socket->pendingDatagramSize() >= rev_length) { ...... } } }}//在mainwindow中调用UDPReceivevoid MainWindow::init_socket(){ udp_socket = new QUdpSocket(this); udp_socket->bind(QHostAddress::Any, UDP_LISTEN_PORT, QUdpSocket::ReuseAddressHint); QHostAddress mcast_addr("224.0.0.17"); //udp_socket->setSocketOption(QAbstractSocket::MulticastLoopbackOption, 0);//禁止本机接收 udp_socket->joinMulticastGroup(mcast_addr); udp_receive = new UDPReceive(udp_socket); udp_receive->start();}
以上程序在运行时报QSocketNotifier: socket notifiers cannot be enabled from another thread错误。
到网上查了查,原因应该是,不能跨线程调用QObject。mainwindow为窗口主线程,UDPReceive为数据传输线程,所以当我们在mainwindow中创建udp_socket,然后通过指针的方式传递给UDPReceive使用,属于跨线程调用QObject。
解决办法为:将新建upd套接字的相关代码移到UDPReceive中,这样就避免了跨线程调用QObject。
void UDPReceive::run(){ QUdpSocket udp_socket; udp_socket.bind(QHostAddress::Any, this->udp_listen_port, QUdpSocket::ReuseAddressHint); QHostAddress mcast_addr("224.0.0.17"); udp_socket.setSocketOption(QAbstractSocket::MulticastLoopbackOption, 0);//禁止本机接收 udp_socket.joinMulticastGroup(mcast_addr); int rev_length = msg_max_length * 2 + 6; while(1) { if (udp_socket.hasPendingDatagrams()) { if (udp_socket.pendingDatagramSize() >= rev_length) { } } }}
- QT中解决QSocketNotifier: socket notifiers cannot be enabled from another thread错误
- QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
- [pyQt_Error]QObject::killTimer: timers cannot be stopped from another thread
- 一个疑惑的问题:QObject::killTimer: Timers cannot be stopped from another thread
- ORB-SLAM2:QObject::~QObject: Timers cannot be stopped from another thread
- java hibernate 中"nodeto traverse cannot be null"错误解决
- Eclipse cannot be resolved错误的解决
- Eclipse May be locked by another process错误解决
- ORA-01618: redo thread 2 is not enabled - cannot mount 错误
- ORA-01618: redo thread 2 is not enabled - cannot mount 错误
- ORA-01618: redo thread 2 is not enabled - cannot mount 错误
- 解决'libproxychains.so.3' from LD_PRELOAD cannot be preloaded问题
- Struts中Resources cannot be null错误
- Struts中Resources cannot be null错误
- Struts中Resources cannot be null错误
- java hibernate 中"node to traverse cannot be null"错误解决
- java hibernate 中"node to traverse cannot be null"错误解决
- java hibernate 中"node to traverse cannot be null"错误解决
- 关于TreeNode.Expanded 属性
- vc2008 fstream不支持中文路径的解决方案
- 为什么要用补码
- VS2008下fstream操作文件时的中文路径
- c++中的引用的使用原理以及使用实例
- QT中解决QSocketNotifier: socket notifiers cannot be enabled from another thread错误
- 几种软件缺陷的可能利用方法
- 获取所有已安装的程序信息
- FLEX调用带结果地图服务的GP服务
- java面试题--程序查错
- 菜鸟入门:Java语言学习
- AsyncTask中执行完doInBackground不执行onPostExecute
- Object C 初识
- 光盘刻录编程