QLocale and QSettings

╄→尐↘猪︶ㄣ 提交于 2019-12-08 03:56:28

问题


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:

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.


回答1:


It seems to be a bug. I've just test you code using 5.6 and macOS Sierra (10.12.3) and it works correctly, even without the hack, but 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);


来源:https://stackoverflow.com/questions/40155440/qlocale-and-qsettings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!