Hi,

I'm facing a small problem with QSettings and storing a python list in
it. It seems that I can't store an empty list properly into a QSettings
object, its always converted to an invalid QVariant somewhere along the
way. The attached sample script shows this, all is well until I create a
new QSettings object from the file and try to get the list again. The
printout of the ini file also shows this.

Specifying the type for the value() call does not work, that raises an
error that the conversion of the QVariant does not work due to different
typeids (0 vs.  256).

Am I doing something wrong or do I really need to serialize the list
manually into a string? (Can't even use QStringList here since thats not
available with the v2 API).

sip: 4.12.1
PyQt: 4.8.3
Qt: 4.7.3

import sip
sip.setapi("QString", 2)
sip.setapi("QDate", 2)
sip.setapi("QDateTime", 2)
sip.setapi("QTextStream", 2)
sip.setapi("QTime", 2)
sip.setapi("QUrl", 2)
sip.setapi("QVariant", 2)

from PyQt4.QtCore import QSettings

s1 = QSettings("test.ini", QSettings.IniFormat)

l1empty = s1.value("Entries", [])
l1nonempty = s1.value("NonEmpty", [])
print "empty list, initial value", l1empty
print "non-empty list, initial value", l1nonempty

# Storing an empty list breaks the on-disk file
s1.setValue("Entries", l1empty)
# A non-empty list stores just fine
s1.setValue("NonEmpty", [1,2])

l1empty = s1.value("Entries", [])
l1nonempty = s1.value("NonEmpty", [])
print "empty list, value setting in memory", l1empty
print "non-empty list, value after setting in memory", l1nonempty

s1.sync()

l1empty = s1.value("Entries", [])
l1nonempty = s1.value("NonEmpty", [])
print "empty list, value after syncing", l1empty
print "non-empty list, value after syncing", l1nonempty

print "Dropping first QSettings object, creating new one from file"

s1 = None
s2 = QSettings("test.ini", QSettings.IniFormat)

# This one is going to be None
l2empty = s2.value("Entries", [])
# This one will read out properly
l2nonempty = s2.value("NonEmpty", [])

print "empty list, value after creating new settings", l2empty
print "non-empty list, value after creating new settings", l2nonempty

print "Ini file contents", open("test.ini").read()

import os
os.remove("test.ini")

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to