QLocale and QSettings
Premise: I am using osx using qt5.7. I changed the decimal separator to System Preferences
- Language and Region
- Advanced
to use a comma:
I have a problem with saving / restoring a value QLocale
using QSettings
.
This is main.cpp
:
#include <QSettings>
#include <QDebug>
void printLocale(QString header, QLocale locale) {
qDebug() <<
QLocale::languageToString(locale.language()) <<
QLocale::scriptToString(locale.script()) <<
QLocale::countryToString(locale.country()) <<
locale.decimalPoint() << "-" << header;
}
int main( int argc, char **argv )
{
QLocale my_loc=QLocale::system();
printLocale("System OK", my_loc);
QSettings my_set("test","");
my_set.setValue("locale",my_loc);
QLocale my_set_loc=my_set.value("locale").toLocale();
printLocale("QSettings NOT OK",my_set_loc);
// hack from /questions/2188148/force-point-and-not-comma-for-floating-point-in-qt/5912954#5912954
QLocale hungary(QLocale::Hungarian);
my_set_loc.setNumberOptions(hungary.numberOptions());
printLocale("Hungarian STILL NOT OK",my_set_loc);
return 0;
}
and this is mine .pro
:
TEMPLATE = app QT += core TARGET = test INCLUDEPATH += . SOURCES += main.cpp
Output:
"English" "Latin" "UnitedStates", "-" System OK "
"English" "Latin" "UnitedStates". - "QSettings NOT OK"
"English" "Latin" "UnitedStates". - "Hungarian NOT APPROVED YET"
and it seems to QLocale
know that I am using a comma as a decimal separator, but when this QLocale
is stored in QSettings
and read, Qt does not restore it.
Also when trying the hacker described here: https://stackoverflow.com/a/2188148/ it doesn't work.
source to share
This seems to be a bug. I just tested your code using 5.6 and macOS Sierra (10.12.3) and it works correctly even without a hack, but when tested on Qt 5.8 it stops working. But if you change the initialization QSettings
to save the settings to a file, it works!
// QSettings my_set("test","");
QSettings my_set("test.ini", QSettings::IniFormat);
source to share