Issue
I am trying to connect to a scanner which offers a REST Api over an apiport. Before I can do that I have to connect to the Scanner using PyQt5 and sign a challange. I have a C++ code as example but I can not find the corresponding PyQt Classes/Methods.
Basically I am looking for a way to replace these lines from C++:
QWebSocket socket;
QString address = "127.0.0.1";
QString port = "1234";
connect(&socket,&QWebSocket::textMessageReceived,this,&Client::onTextMessageReceived);
socket.open(QUrl(QString("ws://%1:%2").arg(address).arg(port)));
My problem is this line:
connect(&socket,&QWebSocket::textMessageReceived,this,&Client::onTextMessageReceived);
Can someone help me on this one? All I have till now is this: (but it throws the Error: TypeError: native Qt signal is not callable)
class Client(QtCore.QObject):
def __init__(self, parent):
super().__init__(parent)
self.client = QtWebSockets.QWebSocket("",QtWebSockets.QWebSocketProtocol.Version13,None)
#self.client.error.connect(self.error)
print("Trigger")
trigger = self.client.textMessageReceived()
#self.client.open(QUrl("ws://"+UDP_IP+":"+str(notificationsport)))
def ontextmsgreceived():
print("Text MSG received")
def close(self):
self.client.close()
global client
app = QApplication(sys.argv)
client = Client(app)
app.exec_()
Thanks a lot for your Help!
Solution
In PyQt the connection syntax is the following:
C++:
connect(sender, &Class_sender::some_signal, receiver, &Class_receiver::some_slot);
python:
sender.some_signal.connect(receiver.some_slot)
so in your case:
self.client.textMessageReceived.connect(self.ontextmsgreceived)
Another problem is that your slot is a method of the class so the first parameter must be the self
, in addition the textMessageReceived signal sends a text as a parameter, so the slot must have the same parameter:
def ontextmsgreceived(self, message):
print("Text MSG received", message)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.