c++qtqsettingsqlocale

QLocale and QSettings


Premise: I'm on osx using qt5.7 I've changed the decimal separator in the System Preferences - Language and Region - Advanced to use the comma:

enter image description here

I have a problem in storing/restoring the QLocale value via QSettings.

This is the 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 https://stackoverflow.com/a/11603299/2743307
    QLocale hungary(QLocale::Hungarian);
    my_set_loc.setNumberOptions(hungary.numberOptions());

    printLocale("Hungarian STILL NOT OK",my_set_loc);

    return 0;
}

and this is my .pro:

TEMPLATE = app
QT += core
TARGET = test
INCLUDEPATH += .
SOURCES += main.cpp

The output is:

"English" "Latin" "UnitedStates" ',' - "System OK"

"English" "Latin" "UnitedStates" '.' - "QSettings NOT OK"

"English" "Latin" "UnitedStates" '.' - "Hungarian STILL NOT OK"

and it looks like the QLocale is aware that I use comma as decimal separator but when this QLocale is stored in QSettings and read back, Qt does not recover it.

Also when trying the hack described here: https://stackoverflow.com/a/11603299/2743307 it doesn't work.


Solution

  • It seems to be a bug. I've just tested your code using 5.6 and macOS Sierra (10.12.3) and it works correctly, even without the hack.

    When testing on Qt 5.8 it stopped working, but if you change the initialization of the QSettings to save settings to a file, it works!

    // QSettings my_set("test","");
    QSettings my_set("test.ini", QSettings::IniFormat);