[PyQt] Can't pass a dict to a signal that expects QJsonObject

Davi Ramos davirtavares at gmail.com
Fri Jul 7 15:15:44 BST 2017

I way to bypass this (pointed out by altendky) is to convert all the values
of the dict to QVariant before sending it to the signal:

d = {k: v.toVariant() for k, v in message.object().items()}
doc = QJsonDocument(d)
self.messageReceived.emit(doc.object(), self) # that works fine

There's also another issue (which could be related or not to this):

d = {'a': 1}
j = PyQt5.QtCore.QJsonDocument(d)
o = j.object()
j = PyQt5.QtCore.QJsonDocument(o)

That fails:

TypeError: 'QJsonValue' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: <class 'PyQt5.QtCore.QJsonDocument'> returned a result with an
error set

Here is a more readable example by altendky:

Davi R. Tavares

On Fri, Jul 7, 2017 at 9:58 AM, Davi Ramos <davirtavares at gmail.com> wrote:

> Versions of PyQT tested: 5.8.2 and 5.9
> Basically there is a signal on QWebChannelAbstractTransport called
> "messageReceived" and it's signature is as follow:
> void messageReceived(const QJsonObject &message,
> QWebChannelAbstractTransport *transport)
> According to PyQt documentation, QJsonObject is internally converted to
> "dict", but when I try to emit that signal passing a dict that was returned
> from QJsonDocument.object(), it fails:
> message = QJsonDocument.fromJson(messageData.encode("utf-8"),
> parser_error)
> self.messageReceived.emit(message.object(), self)
> Traceback (most recent call last):
>   File "main.py", line 54, in textMessageReceived
>     self.messageReceived.emit(a, self)
> TypeError: messageReceived(self, Dict[str, Union[QJsonValue,
> QJsonValue.Type, Iterable[QJsonValue], bool, int, float, str]],
> QWebChannelAbstractTransport).emit(): argument 1 has unexpected type
> 'dict'
> Aborted
> I tried passing an empty dict {} and it worked (although it failed later
> cause of other reasons, but the signal was emitted).
> A better context is provided here: https://pastebin.com/knJr26Rj
> --
> Davi R. Tavares
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170707/2b803545/attachment.html>

More information about the PyQt mailing list