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.