Dup Ver Goto 📝

PySide6Network1

PT2/aw/dev/qt does not exist
To
28 lines, 95 words, 966 chars Page 'PySide6Network1' does not exist.

Receive Udp

from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtNetwork import *

class MyWidget(QWidget):
    def handleUdp(self):
      while self.udpSocket.hasPendingDatagrams():
        datagram = self.udpSocket.receiveDatagram(4096)
        data = datagram.data().data()
        self.process_data(data.decode())

    def __init__(self, parent=None):
        super().__init__(parent)

        self.udpSocket = QUdpSocket()
        self.udpSocket.readyRead.connect(self.handleUdp)
        self.udpSocket.bind(QHostAddress.Any,in_port)

    def process_data(self,data):
        # do something with data

Notes

if bind fails, it returns False, rather than raising an exception. The usual cause of bind failing is that the address is already in use. This can be useful if you only want one instance of a program running. See for example sleepqt.