QSocketNotifier:套接字通知程序不能从另一个线程启用或禁用 [英] QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread

查看:6529
本文介绍了QSocketNotifier:套接字通知程序不能从另一个线程启用或禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试构建一个使用QT的多线程游戏服务器,因此,每个客户端都是基于独立线程的"QThread".现在,我需要将数据发送到同一游戏室中的其他客户端(在其他线程中).例如,当房主关闭游戏室时,游戏服务器需要将"room_closed"消息发送给其他客户端,但消息却相同:

I try to build a Multi Threading Game Server used QT,so, every client is a standalone thread based "QThread".now I need to send data to other clients(in other thread) whichs in the same game room. example,When a house-owner closed the game room, the Game Server needs to send to the "room_closed" message to other clients whichs in the same soom, but there is an error :

QSocketNotifier:无法从以下位置启用或禁用套接字通知程序 另一个线程

QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread

ps:我尝试使用正弦/插槽,但仍然存在错误:

ps: I tryed to use the sinals/slots but there is still a error:

QObject:无法为处于不同状态的父级创建子级 线. (父母是QNativeSocketEngine(0x161764e8),父母的线程是 ClientThread(0x16196f10),当前线程为QThread(0x14a17278)

QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0x161764e8), parent's thread is ClientThread(0x16196f10), current thread is QThread(0x14a17278)

我该怎么办?

推荐答案

似乎您不能正确处理QObjects和QThreads,而Qt对此有所抱怨.

It seems you do not handle QObjects and QThreads properly and Qt complains about it.

我建议您看看Qt文档:

I suggest you take a look at Qt documentation:

  • Threads and QObject
  • QObject thread affinity

快速指南:

  • 每个QObject实例都与一个QThread(QObject::thread())相关联.
  • 您可以使用QObject::moveTothread()更改其关联的线程,但是QObject必须没有父对象,并且必须从与QObject当前关联的线程进行调用.
  • 某些QObject(os子类)具有无法从与其关联的线程之外的其他线程调用的函数.这就是为什么您遇到关于QSocketNotifier的第一个错误的原因.
  • 您不能从一个线程创建QObject并为其提供与另一个线程关联的父对象.那是你的第二个错误.
  • Each instance of QObject is associated with a QThread (QObject::thread()).
  • You can change its associated thread by using QObject::moveTothread(), but the QObject must not have a parent and the call must be made from the thread the QObject is currently associated with.
  • Some QObject (os sub classes) have functions that cannot be called from another thread than the thread they are associated with. That is why you have your first error about QSocketNotifier.
  • You cannot create a QObject from a thread and give it a parent that is associated with another thread. That is your second error.

这篇关于QSocketNotifier:套接字通知程序不能从另一个线程启用或禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆