Hey guys I have this problem with Qsettings when using Pyside. Whenever I try to retrieve a list which was stored earlier... I get back unicode. I've searched the problem online but there doesn't seem to be a solution concerning lists.
Asked
Active
Viewed 529 times
0
-
Thanks.. I'll try that out. Here's my code: settings = QtCore.Qsettings() notes = settings.value('recent_note_list', []) print type(notes) >>>>
– Michael Aug 07 '14 at 13:15 -
1Sorry, didn't see your comment here. You could have also commented on my answer. Anyway, you can't store arbitrary python objects inside QSettings. Many languages have Qt bindings, and QSettings is abstracting over different formats. In case you want to store highly complex data, you could serialize it to JSON first and store it in a single value. Kinda defeats the purpose of QSettings' abstraction though. – Reiner Gerecke Aug 14 '14 at 13:43
1 Answers
2
According to the PySide documentation, in order to store a list/array in a QSettings Object, you need to do this:
mylist = ['a', 'b', 'c']
settings = QSettings()
settings.beginWriteArray("mylist")
for idx, value in enumerate(mylist):
settings.setArrayIndex(i)
settings.setValue("key", value)
settings.endArray()
As shown here: http://srinikom.github.io/pyside-docs/PySide/QtCore/QSettings.html#PySide.QtCore.PySide.QtCore.QSettings.beginWriteArray
To read it:
mylist = []
settings = QSettings()
size = settings.beginReadArray("mylist")
for i in range(size):
settings.setArrayIndex(i)
mylist.append(settings.value("key"))
settings.endArray()
As shown here: http://srinikom.github.io/pyside-docs/PySide/QtCore/QSettings.html#PySide.QtCore.PySide.QtCore.QSettings.beginReadArray
Perhaps you can show a sample of your code, so we can offer more help.

Reiner Gerecke
- 11,936
- 1
- 49
- 41