0

I am converting my simple Udp Client application which is working on the Qt C++ but when I try to achieve the same behaviour on the Pyside6 readyRead is not triggered even though the socket seems to be in the ConnectedState.

I am using Windows machine.

Pyside6 Code - Not triggering readyRead

import sys

from PySide6.QtCore import QDataStream, QTimer, Qt, QObject, Signal
from PySide6.QtGui import QIntValidator
from PySide6.QtNetwork import QAbstractSocket, QUdpSocket, QHostAddress
from PySide6.QtWidgets import (QApplication, QDialog, QDialogButtonBox, QGridLayout,
                               QLabel, QLineEdit, QMessageBox, QPushButton,
                               QVBoxLayout, QWidget)

# from UdpRx import UdpConectionManager

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

        self._block_size = 0
        self._current_fortune = ''

        host_label = QLabel("&Server name:")
        port_label = QLabel("S&erver port:")

        self._host_line_edit = QLineEdit('Localhost')
        self._port_line_edit = QLineEdit()
        self._port_line_edit.setValidator(QIntValidator(1, 65535, self))

        host_label.setBuddy(self._host_line_edit)
        port_label.setBuddy(self._port_line_edit)

        self._status_label = QLabel("Data: ")


        self.hostname = "127.0.0.1"
        self.portnum = 12000

        self._udp_socket_manager = UdpConectionManager(self.hostname, self.portnum)

        self._udp_socket_manager.ProcessTheDataGram.connect(self.process_Data)

        main_layout = QGridLayout(self)
        main_layout.addWidget(self._status_label, 2, 0, 1, 2)

        self.setWindowTitle("Fortune Client")
        self._port_line_edit.setFocus()


    def process_Data(self, data):
        self._status_label.setText(data)


class UdpConectionManager(QObject):
    ProcessTheDataGram = Signal(str)
    
    def __init__(self, hostname, portnum):
        super().__init__()
        self.udp_socket_ptr = QUdpSocket(self)
        print("Making socket connection to : ", portnum)
        self.udp_socket_ptr.bind(QHostAddress(hostname), int(portnum))
        self.udp_socket_ptr.readyRead.connect(self.readPendingDataGrams)

    def readPendingDataGrams(self):
        while self.udp_socket_ptr.hasPendingDatagrams():
            datagram = self.udp_socket_ptr.receiveDatagram()
            self.ProcessTheDataGram.emit(str(datagram.data()))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    client = UdpClient()
    client.show()
    sys.exit(client.exec())

I get the results when I execute this routine as Qt C++ Application

#include <QtNetwork/QUdpSocket>
#include <QtNetwork/QNetworkDatagram>
#include <QObject>
#include <qdebug.h>

class UdpConnectionManager : public QObject
{
    Q_OBJECT

public:
    UdpConnectionManager(QString host_name, quint16 port_number) : udp_sockter_ptr(new QUdpSocket(this))
    {
        udp_sockter_ptr->bind(QHostAddress(host_name), port_number);
        connect(udp_sockter_ptr,&QUdpSocket::readyRead,this, &UdpConnectionManager::ReadPendingDataGrams);
    }

    void ReadPendingDataGrams()
    {
        while (udp_sockter_ptr->hasPendingDatagrams()) {
            QNetworkDatagram datagram = udp_sockter_ptr->receiveDatagram();
            QString data_gram = datagram.data();
            emit ProcessTheDataGram(data_gram);
        }
    }
Q_SIGNALS:
    void ProcessTheDataGram(QString& data);


private:
    QString host_name;
    quint16 port_number;
    QUdpSocket* udp_sockter_ptr;

};

I was also surprised not to find example codes for the QUdpSocket in the Pyside6.

Thanks in advance.

bgorkhe
  • 321
  • 3
  • 13
  • 2
    You should make a minimal reproducible example. I tried this with `PyQt5` and it works, I added the imports and made a `QCoreApplication` to run it. I have no access to PySide6 but I doubt it does anything substantially different. – Bert Oct 03 '21 at 15:16
  • please provide a [mre] – eyllanesc Oct 03 '21 at 16:19
  • Did you test the C++ code with Qt5 or Qt6? – eyllanesc Oct 03 '21 at 16:19
  • @eyllanesc just updated the example with the application code. If I include my UdpConectionManager class in the same file, I am receiving the data but when I import using another file somehow it was not getting data. Thanks in advance. – bgorkhe Oct 03 '21 at 16:38
  • @eyllanesc Qt5 I m using on the c++ app – bgorkhe Oct 03 '21 at 16:46
  • Maybe something changed in Qt6 so that could explain the different behavior, can you try Qt6 to confirm my hypothesis? – eyllanesc Oct 03 '21 at 16:52
  • @eyllanesc when I added the UdpConnectionManager class in the same file it works but when I try to import this class it does not. Pyside6 uses Qt6. Do you know if importing QObject based classes got issues? – bgorkhe Oct 04 '21 at 10:55

0 Answers0