2

I've saved dict with QSetting, and when I load it i get QVariant

>>dict
{u'key1': u'val1', u'key2': u'val2',....}
...
>>loadedDict
<PyQt4.QtCore.QVariant object at 0x02B11848>

How to convert it back to dict so I can use it like this again:

>>dict['key2']
val2
Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • 2
    Does the `loadedDict.toPyObject()` answer to [this question](http://stackoverflow.com/questions/2333420/how-do-i-get-my-python-object-back-from-a-qvariant-in-pyqt4) help? – DSM Feb 01 '13 at 12:36
  • No it doesn't, or I don't know how to use it : >>loadedDict.toPyObject() gives this: – Aleksandar Feb 01 '13 at 14:23

1 Answers1

3

You can convert the dict into a string and after just eval(str) it back to a dict:

pydict  = {'key1': 'val1', 'key2': 'val2'}
variant = QtCore.QVariant( pydict )
...
pydict = eval( str( variant.toString() ) )
print pydict
>>> {'key1': 'val1', 'key2': 'val2'}

Just keep in mind the eval could fail if it's not able to convert one of the keys or values from string to dict. this can happen if you have a none built_in types (str, int, list.. ) but in your case it will work no problems.

Asi
  • 116
  • 1
  • 9