<div dir="ltr"><div><div><div>Is QWebChannel supported by PyQt5?<br></div>Beucase i tried to port the chat official example of Qt and blocked when implementing the WebSocketTransport class.<br></div><div>See <a href="https://code.qt.io/cgit/qt/qtwebchannel.git/tree/examples/webchannel/shared/websockettransport.cpp">https://code.qt.io/cgit/qt/qtwebchannel.git/tree/examples/webchannel/shared/websockettransport.cpp</a><br><br></div><div>The following code print this error when trying to emit the messageReceived signal<br><br>TypeError: messageReceived(self, Dict[str, Union[QJsonValue, QJsonValue.Type, Iterable[QJsonValue], bool, int, float, str]], QWebChannelAbstractTransport).emit(): argument 1 has unexpected type 'dict'</div><div><br></div><span style="color:rgb(0,0,128);font-weight:bold">class </span>WebSocketTransport(QWebChannelAbstractTransport):<br><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"dejavu sans mono";font-size:9.3pt"> <span style="color:rgb(0,0,128);font-weight:bold">def </span><span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(148,85,141)">self</span>, logger, server_socket, client_socket, *args, **kwargs):<br> <span style="color:rgb(0,0,128)">super</span>(WebSocketTransport, <span style="color:rgb(148,85,141)">self</span>).<span style="color:rgb(178,0,178)">__init__</span>(*args, **kwargs)<br> <span style="color:rgb(148,85,141)">self</span>.logger = logger<br> <span style="color:rgb(148,85,141)">self</span>.server_socket = server_socket<br> <span style="color:rgb(148,85,141)">self</span>.client_socket = client_socket<br> <span style="color:rgb(148,85,141)">self</span>.client_socket.textMessageReceived.connect(<span style="color:rgb(148,85,141)">self</span>.on_text_message_received)<br> <span style="color:rgb(148,85,141)">self</span>.client_socket.disconnected.connect(<span style="color:rgb(148,85,141)">self</span>.on_disconnected)<br><br> <span style="color:rgb(0,0,128);font-weight:bold">def </span>sendMessage(<span style="color:rgb(148,85,141)">self</span>, message, *args, **kwargs):<br> <span style="color:rgb(148,85,141)">self</span>.logger.debug(message)<br> document = QJsonDocument()<br> document.setObject(message)<br> document_as_bytes = document.toJson(QJsonDocument.Compact)<br> document_as_str = document_as_bytes.decode(<span style="color:rgb(0,128,128);font-weight:bold">'utf-8'</span>)<br> <span style="color:rgb(148,85,141)">self</span>.client_socket.sendTextMessage(document_as_str)<br><br> <span style="color:rgb(0,0,128);font-weight:bold">def </span>on_text_message_received(<span style="color:rgb(148,85,141)">self</span>, message):<br> <span style="color:rgb(148,85,141)">self</span>.logger.debug(<span style="color:rgb(0,128,128);font-weight:bold">'Message:' </span>+ message)<br> document = QJsonDocument()<br> error = <span style="background-color:rgb(228,228,255)">QJsonParseError</span>()<br> document = document.fromJson(message.encode(<span style="color:rgb(0,128,128);font-weight:bold">'utf-8'</span>), error)<br> <span style="color:rgb(0,0,128);font-weight:bold">if </span>error.error:<br> <span style="color:rgb(148,85,141)">self</span>.logger.debug(<span style="color:rgb(0,128,128);font-weight:bold">'Failed to parse ' </span>+ message + <span style="color:rgb(0,128,128);font-weight:bold">'as Json object'</span>)<br> <span style="color:rgb(148,85,141)">self</span>.logger.debug(<span style="color:rgb(0,128,128);font-weight:bold">'Error is ' </span>+ error.errorString())<br> <span style="color:rgb(0,0,128);font-weight:bold">return<br></span><span style="color:rgb(0,0,128);font-weight:bold"> if not </span>document.isObject():<br> <span style="color:rgb(148,85,141)">self</span>.logger.debug(<span style="color:rgb(0,128,128);font-weight:bold">'Message is not an object'</span>)<br> <span style="color:rgb(0,0,128);font-weight:bold">return<br></span><span style="color:rgb(0,0,128);font-weight:bold"> </span><span style="color:rgb(148,85,141)">self</span>.messageReceived.emit(document.object(), <span style="color:rgb(148,85,141)">self</span>)<br><br> <span style="color:rgb(0,0,128);font-weight:bold">def </span>on_disconnected(<span style="color:rgb(148,85,141)">self</span>):<br> <span style="color:rgb(148,85,141)">self</span>.server_socket.on_client_disconnected(<span style="color:rgb(148,85,141)">self</span>)</pre></div><div><div><div><div><div><br><br>Is it possible to have that official example implemented in the pyqt5 examples?<br><br>-- <br><div class="gmail_signature">Filippo Cucchetto</div>
</div></div></div></div></div></div>