问题描述
前提:我正在使用qt5.7在osx上,我已经将System Preferences
-Language and Region
-Advanced
中的小数点分隔符更改为使用逗号:
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:
我在通过QSettings
存储/恢复QLocale
值时遇到问题.
I have a problem in storing/restoring the QLocale
value via QSettings
.
这是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;
}
这是我的.pro
:
TEMPLATE = app
QT += core
TARGET = test
INCLUDEPATH += .
SOURCES += main.cpp
输出为:
英语"; 拉丁" 美国" '.' -"QSettings NOT OK"
"English" "Latin" "UnitedStates" '.' - "QSettings NOT OK"
英语"; 拉丁" 美国" '.' -匈牙利仍然无法正常运行"
"English" "Latin" "UnitedStates" '.' - "Hungarian STILL NOT OK"
并且看起来QLocale
知道我使用逗号作为小数点分隔符,但是当将此QLocale
存储在QSettings
中并回读时,Qt无法恢复它.
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.
另外,当尝试此处所述的黑客攻击时: https://stackoverflow.com/a/11603299/2743307 它不会不行.
Also when trying the hack described here: https://stackoverflow.com/a/11603299/2743307 it doesn't work.
推荐答案
这似乎是一个错误.我刚刚使用5.6和macOS Sierra(10.12.3)对您的代码进行了测试,即使没有黑客攻击,它也可以正常工作,但是在Qt 5.8上进行测试时,它停止了工作.但是,如果您更改QSettings
的初始化以将设置保存到文件中,它将起作用!
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);
这篇关于QLocale和QSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!