[PyQt] A possible bug in PyQt when loading Boolean values via QSettings
Phil Thompson
phil at riverbankcomputing.com
Mon Dec 27 10:04:53 GMT 2010
On Sun, 26 Dec 2010 20:30:28 +0000, Baz Walter <bazwal at gmail.com> wrote:
> On 25/12/10 10:16, Phil Thompson wrote:
>> It's not a bug. You can't make assumptions about the capabilities of
the
>> backend (platform specific) storage regarding saving type information.
>> You
>> have to explicitly test for the values that might come back.
>
> are you saying that portability may be compromised when using the
> QVariant v2 api with QSettings?
It depends on the QSettings backend (if any of the backends behave
differently).
> with the QVariant v1 api, QSettings will always return a QVariant - so
> there is a consistent (and portable) way to deal with whatever values
> are delivered by the backend:
>
> settings.py
> ===========
> from PyQt4.QtCore import QSettings
>
> settings = QSettings('foo', 'foo')
> settings.clear()
> key = 'section/item'
> print('item = %s' % repr(settings.value(key)))
> settings.setValue(key, False)
> print('item = %s' % repr(settings.value(key)))
>
> # re-sync with storage
> settings = QSettings('foo', 'foo')
> print('item = %s' % repr(settings.value(key)))
> ============
>
> $ python2 settings.py
> item = <PyQt4.QtCore.QVariant object at 0xb743aed4>
> item = <PyQt4.QtCore.QVariant object at 0xb743aed4>
> item = <PyQt4.QtCore.QVariant object at 0xb743aed4>
>
> $ python3 settings.py
> item = None
> item = False
> item = 'false'
>
> i am only able to test this on linux at the moment.
>
> but might i get different output from python3 if i ran this on windows?
A typical backend will save values as a string without any metadata
describing the original type. In QVariant v1 the conversion back is done by
QVariant::toBool() which only checks for "true" and "false", so that is the
behaviour to be replicated in QVariant v2.
If this is a problem then I could add an optional keyword argument to
QSettings.value() that takes a Python type argument and will automatically
return a value of that type or raise an exception...
b = settings.value(key, type=bool)
...or any other suggestions?
Phil
More information about the PyQt
mailing list