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

Phil Thompson phil at riverbankcomputing.com
Mon Jul 10 17:59:04 BST 2017


On 7 Jul 2017, at 3:15 pm, Davi Ramos <davirtavares at gmail.com> wrote:
> 
> 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: https://gist.github.com/altendky/a64a7094524c68734de6fb59727d67b0

Should be fixed in tonight's snapshot.

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

That code is incomplete. If the above fix doesn't work for this problem can you send be a test that I can run?

Thanks,
Phil


More information about the PyQt mailing list