How to use QWebSocket, QTcpSocket globally across the application?

  • I need to send packets from other classes, should I register a slot in my QTcpSocket class like "send" connect it and emit from other classes? Or is there a better way? Can I just use singleton like:

    QTcpSocket g_tcpSocket;

  • Usually you'd create a custom class like MyNetworkHandler that then can be a singleton if you wish. That class with have the corresponding methods to send the data you need to send from the different parts of the application.

    Of course there are other solutions as well - it really depends on your needs and the design of the existing parts of the application. The above is just a good, common approach (in my opinion).

  • @Joel-Bodenmann MyNetworkHandler containing QTcpSocket member? Does it mean I can directly call it without using signals?

  • It really depends on your needs. Theoretically you can make MyNetworkHandler become a subclass of QTcpSocket and just make it a singleton. If you need more flexibility then MyNetworkHandler can just have a QTcpSocket instance as a private handler. In that case you can still add your own signals and slots and/or forward those from QTcpSocket where applicable.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.