[PyQt] Strange behaviour with string conversions when emitting signals with dicts

Phil Thompson phil at riverbankcomputing.com
Wed Apr 7 18:04:38 BST 2010


On Wed, 7 Apr 2010 09:57:39 +0200, Stefan Scherfke
<stefan.scherfke at uni-oldenburg.de> wrote:
> Hello,
> 
> yesterday I noticed a strange behavior with PyQt4 when I emit a signal,
> that has a dict with string-keys as argument.
> 
> Take the following example:
> 
>     from PyQt4 import QtCore
> 
> 
>     class A(QtCore.QObject):
> 
>         sig = QtCore.pyqtSignal(dict)
> 
>         def run(self):
>             self.sig.emit({'a': [1, 'b']})
> 
> 
>     class Main(object):
>         def __init__(self):
>             self.obj = A()
>             self.obj.sig.connect(self.echo)
>             self.obj.run()
> 
>         def echo(self, obj):
>             print obj
> 
> 
>     if __name__ == '__main__':
>         Main()
> 
> 
> Until PyQt 4.7 (on my Mac and until last week on my other machine), the
> (expected) output was:
> 
>     {'a', [1, 'b']}
> 
> But since I got an update to PyQt 4.7.2 on my Kubuntu machine, the
> (unexpected) output is:
> 
>     {PyQt4.QtCore.QString(u'a'): [1, PyQt4.QtCore.QString(u'b')]}
> 
> If I change the signal definition in class B to
> 
>     sig = QtCore.pyqtSignal('PyQt_PyObject')
> 
> the output is again:
> 
>     {'a', [1, 'b']}
> 
> But "pyqtSignal(dict)" was a much more intuitive/pythonic way to define
the
> signal.
> 
> Was this intuitive behavior of PyQt <= 4.7 a bug that was fixed in 4.7.2
or
> is this a new bug introduced with PyQt 4.7.2?
> 
> I can’t imagine that this change is a new “feature”, because it
broke
> my whole application which should not happen with a bugfix release like
> 4.7.2.

It's a bug fix. Dicts with string keys are converted to a native QVariant
rather than being left as a Python object (because you may be trying to
pass data to some C++ code). On the conversion back it will leave those
strings as QStrings.

Phil


More information about the PyQt mailing list