Bool becomes QString when QSettings load

I met a problem when storing bool values in QSettings. It looks like a bug to me, but maybe I'm missing something?

I have this piece of code:

QSettings* settings = new QSettings();
qDebug() << "test setting is of type " << settings->value("test", false).typeName();
settings->setValue("test", true);qDebug() << "now test setting is of type " << settings->value("test", false).typeName();

In the first round, it will print

test parameter is of type bool

test setting is now of type bool

After that, it will print

test parameter is of type QString

test setting is now of type bool

In the first round, there is no stored definition, it takes the default value spent, but after the setting has been set once, it seems that it is stored as a QString and load as a QString on the application next run.

It is ruin a lot of things especially during the passage of the QVariant to QML.

My ugly solution right now is to make a

settings->setValue('test', settings->value('test', false).toBool())

the launch for each Boolean parameter that I have.

Anyone else seeing this?

This behavior is expected although not very intuitive.
QSettings stores type information in ini files. Always ask the specific type when accessing QSetting using the. toBool(), toInt(), toString() etc.

I think it is more correct than the use of the workaround above because there is no guarantee on how QSettings stores data internally.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for