Eckler
Eckler

Reputation: 61

QSettings with different types

I'm trying to store some parameters with different type using QSettings (ini file, Linux).
When I'm reading values from this using settings.value() I just get QStrings.
Are there any way to store type of QVariant inside this settings file or should I use other storage instead?

Thanks in advance!

Upvotes: 2

Views: 1201

Answers (2)

strubbly
strubbly

Reputation: 3477

According to the docs, the QSettings::value() function returns a QVariant. This should then be converted to whatever type you want.

You can use QVariant::type() to find the type you stored in the QSettings.

This is how QSettings should work, and does, using the NativeFormat on Windows.

However, when using an ini file and for most simple types - including strings and integers - QSettings uses the same simple representation in the ini file and therefore merges these types. They all come back as QString type in the QVariant.

More complex types are properly encoded in the ini file and so these types are preserved.

Upvotes: 5

Orest Hera
Orest Hera

Reputation: 6776

As noted in QSettings does not differentiate between string and int values

Every value of QVariant which is read from a .ini file by QSettings has string type.

There is also proposed solution to manually append type information to stored strings. So, if during reading the special postfix is not found it is possible to manually decide regarding conversion to int or to bool.

By the way, for more complex types (like QSize) the type information is stored, so QVariat type is correct.

Upvotes: 1

Related Questions