0
votes

I am writing a server/client code with PyQt5 sockets and I met a strange behavior First, I derived a class from QTCPSocket, so that I abstract the usage of socket to my data frame, use encryption,..etc before sending the data So, let this class = mySocket which is an inherited class from QTCPSocket

mySocket has some variables in its init, Ex: self.key. And as I do in all sockets, I connected its readyread signal to my slot of name: rxdata

Now, the problem. inside rxdata, when I try to get the sender object ( using self.sender() ), what it returns is object of type QTCPSocket not as I was expecting a mySocket object. Which I don't understand

I tried to cast the QTCPsocket returned using qtcpsocketObj.class =mySocket but the problem now, is mySocket.init() obviously not called, this the variables like self.key won't be defined.

What can I do to overcome this issue?

1
Could you provide us a minimal, reproducible example?musicamante
Works fine for me using pyqt 5.13.0. What specific version of pyqt are you testing with?ekhumoro
what do u mean by fine? sender returns the derived class + u can access its variables (defined in __init__() )from the returned object ?Mohammed B.
@MohamedIbrahim Yes - it sends the right class and user-defined attributes are accessible. And I repeat: what specific version of pyqt5 are you testing with?ekhumoro
How to know ? I only piped instll pyqt5Mohammed B.

1 Answers

0
votes

After a lot of search and debugging,

the problem wasn't from self.sender(). the problem was that the QTCPServer object returns QTCPSocket, and casting it using _ class _() as I did wasn't the right way.

The solution was to derive a class from QTCPServer and instead of making it return QTCPSocket, it will return mySocket class object ( the details is well explained in documentation) Here is a sample code:

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

def incomingConnection(self,socketDescriptor):
        newSock = mySocket(self) 
        newSock.setSocketDescriptor(socketDescriptor)
        self.addPendingConnection(newSock)