QSettings value type=int: big positive numbers converted in negative numbers

Baz Walter bazwal at gmail.com
Tue Apr 16 15:48:33 BST 2024


On 16/04/2024 11:23, Gottfried Müller wrote:
> Hello,
> 
> reading an integer value with a huge value (for example: 2845924041) is
> converted in a negative value (-1449043255) using
> 
> settings.value("bigInteger", type=int)
> 
> As a workaround I read this value with type=str and convert it in the
> Python code to an integer value. Is there a way getting the right
> positive value by settings.value?


The type argument can be a string representing one of the Qt basic types:

     https://doc.qt.io/qt-6/qttypes.html

These are guaranteed to be the same size on all platforms, so you could do:

     settings.value("bigInteger", type="quint64")

... just so long as your values are never larger than 2 ** 64 - 1 (or 2 ** 63 -1 for signed integers).


Note that it's *not safe* to omit the type argument, because on some platforms integer values will always be returned as 
strings (i.e. the original type information isn't preserved). In addition, integer values greater than 2 ** 64 - 1 may 
be serialised using a special @Variant syntax (or at least they will be when using INI files - I'm not sure about the 
other formats). For these values, the type argument *must* be omitted, because PyQt writes them using a custom PyObject 
meta-type (i.e. if anything other than type=object is specified, a TypeError will be raised).



More information about the PyQt mailing list