[ACCEPTED]-How to get the original python data from QVariant-qvariant

Accepted answer
Score: 15

You can work around this issue by wrapping 1 your data in an immutable container:

>>> from PyQt4.QtCore import QVariant
>>> data = {'key1': 123, 'key2': 456}
>>> v = QVariant((data,))
>>> v.toPyObject()[0]
{'key2': 456, 'key1': 123}
Score: 0

Before you can compare data1 and data3 you need to 7 convert the QString to Python string simply writing:

>>> same_as_data1 = str(data3)

Now 6 you've got back the same string:

>>> data1 == data
data1 == same_as_data1

Wherever 5 a string appears (as key or values) it will 4 be mapped to QString. You can do the conversion 3 either manually, for example:

d = QVariant({'key1':123}).toPyObject()
d_with_str_keys = dict((str(k), v) for k,v in d.iteritems())

or you can 2 change the QString behaviour, as Avaris pointed 1 out above.

More Related questions