QLocale and QSettings

时间:2016-10-20 13:11:07

标签: c++ qt qsettings qlocale

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.

1 个答案:

答案 0 :(得分:0)

这似乎是一个错误。我只是使用5.6和macOS Sierra(10.12.3)测试你的代码并且它正常工作,即使没有黑客攻击,但是当在Qt 5.8上进行测试时它停止工作。但是,如果您更改QSettings的初始化以将设置保存到文件,它就可以工作!

// QSettings my_set("test","");
QSettings my_set("test.ini", QSettings::IniFormat);
相关问题