[PyQt] Unsatisfactory behaviour of QSettings

Phil Thompson phil at riverbankcomputing.com
Fri May 30 14:41:27 BST 2014


On 29/05/2014 7:59 pm, Baz Walter wrote:
> Hello
> 
> When using the v1 API with QSettings, it's easy to get invariant
> output - even with empty, or single-element lists:
> 
>>>> s = QSettings('test', 'test')
>>>> s.setValue('key', [])
>>>> s.sync()
>>>> v = s.value('key')
>>>> v.isNull()
> True
>>>> list(v.toStringList())
> []
>>>> 
>>>> s.setValue('key', ['a'])
>>>> s.sync()
>>>> list(v.toStringList())
> [PyQt4.QtCore.QString(u'a')]
> 
> But things get really tricky if you try to specify the type and/or use
> the v2 API:
> 
>>>> s.setValue('key', [])
>>>> s.sync()
>>>> v = s.value('key', [], 'QStringList')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unable to convert a QVariant of type 0 to a QMetaType of 
> type 11
>>>> 
>>>> s.setValue('key', ['a'])
>>>> s.sync()
>>>> v = s.value('key', [], 'QStringList')
>>>> list(v.toStringList())
> [<PyQt4.QtCore.QStringList object at 0x1a5c500>]
> 
> Raising a TypeError here seems a bit trigger-happy. I suppose the
> reason why it's happening is because QVariant.canConvert will always
> return False if the variant is null, even though the conversion is
> actually valid. According to the Qt docs, this is done for "historical
> reasons" - but even so, it's still there in Qt5.
> 
> The list-within-list for single-element lists is much harder to
> explain, though - could it be a PyQt bug?
> 
> Whatever the causes, these two issues make it very awkward to get
> invariant output when using the v2 API.
> 
> Is it too late to try to improve things?

I can't reproduce this using any of the Python2/PyQt4, Python3/PyQt4 and 
Python3/PyQt5 combinations.

Phil


More information about the PyQt mailing list