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?
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)