PySide Bugzilla Closed for New Bugs

PySide is now a Qt Add-on and uses the Qt Project's JIRA Bug Tracker instead of this Bugzilla instance. This Bugzilla is left for reference purposes.

Bug 459 - Unable to gets boolean values from an INI file
: Unable to gets boolean values from an INI file
Status: CLOSED DUPLICATE of bug 345
Product: PySide
Classification: Unclassified
Component: QtCore
: HEAD
: PC Linux
: P3 major
Assigned To: renato filho
:
:
:
  Show dependency treegraph
 
Reported: 2010-11-05 18:06 EET by Hereldar
Modified: 2010-11-26 06:33 EET (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hereldar 2010-11-05 18:06:40 EET
Steps to Reproduce:
-------------------

    1) Force INI format and save the boolean value:

        QSettings.setDefaultFormat(QSettings.IniFormat)

        settings = QSettings()
        settings.setValue("key", False)
        settings.sync()

    2) Load settings and recovery "key" value:

        QSettings.setDefaultFormat(QSettings.IniFormat)

        settings = QSettings()
        value = settings.value("key")

        print repr(value)
        print bool(value)

Actual Results:
--------------

    u'false'
    True

Expected Results:
-----------------

    False
    False

Platform:
--------

    Kubuntu 10.10
    KDE S.C. 4.5.1.
    Qt. 4.7.0
    PySide 0.4.1
Comment 1 Hereldar 2010-11-09 09:34:56 EET
It also happens the same error with integer values, can not find the data type
and returns a string. In this case is less serious because only is necessary to
call the function "int()".

However, the byte arrays are detected and correctly returns a QByteArray
object.
Comment 2 Matti Airas 2010-11-09 09:45:47 EET
I believe this is a duplicate of bug 345 and bug 278. In essence, in C++
QSettings doesn't store the setting type but only retuns a QVariant containing
a QString, and when that's mapped back to Python, it's always a QString. So,
unfortunately, you need to cast the return value to a correct type yourself.

Please see the description of 345, Renato described the problem there in depth.

*** This bug has been marked as a duplicate of bug 345 ***
Comment 3 Hereldar 2010-11-09 10:18:45 EET
If I have not misunderstood, the problem is that "QSettings" creates a
"QVariant" with the value of the variable in the INI file. By not checking the
type of the variable, this value is always a string and, therefore, if you run
"QVariant.type()" this would return "Variant::String".

This is no problem in the original QT API because it requires specifying the
type of data to be obtained. However, yes it is on PySide because it uses the
internal data type "QVariant ()" to convert to the native type on "Python".

Could not modify "QSettings" and force to create "QVariant" objects with the
right type? It would be a much better solution and would save confusion for
newcomers.
Comment 4 Hugo Parente Lima 2010-11-09 13:25:16 EET
(In reply to comment #3)
> If I have not misunderstood, the problem is that "QSettings" creates a
> "QVariant" with the value of the variable in the INI file. By not checking the
> type of the variable, this value is always a string and, therefore, if you run
> "QVariant.type()" this would return "Variant::String".
> 
> This is no problem in the original QT API because it requires specifying the
> type of data to be obtained. However, yes it is on PySide because it uses the
> internal data type "QVariant ()" to convert to the native type on "Python".
> 
> Could not modify "QSettings" and force to create "QVariant" objects with the
> right type? It would be a much better solution and would save confusion for
> newcomers.

The problem is that there's no way to know what's the right type, syntactally
speaking the right type is always QString, in C++ you transform it to the
semantically correct type using QVariant methods like toInt, toDouble, etc...
the same should be done in python, but with the factory functions, int(),
float(), etc.

In other words, there no way to know if the programmer wants the string "25"
read by QSettings (yes, it always read strings) to be the integral number 25,
the floating point 25.0 or the string "25".

*** This bug has been marked as a duplicate of bug 345 ***
Comment 5 Hereldar 2010-11-09 13:54:38 EET
That problem was solved many frameworks (eg PHP, which are the ones I know) as
follows:

Original:          Saved:              Recovered:
int(0)            0                   int(0)
float(0)          0.0                 float(0)
str(0)            "0"                 unicode(0)
unicode(0)        "0"                 unicode(0)
bool(0)           False               bool(False)

If you want to PySide is comfortable to use and have decided to remove
QVariant(), use the system I just described seems best solution.

I say this especially to the Booleans: bool(), that would be a Pythonic
solution, does not work. In the case of integers or floats, to use int() or
float() is already fixed.
Comment 6 Hugo Parente Lima 2010-11-25 17:53:43 EET
Released on version 1.0.0~beta1
Comment 7 Hereldar 2010-11-26 06:33:47 EET
(In reply to comment #6)
> Released on version 1.0.0~beta1

Thanks a lot for your work.