Qt 是否强制使用系统语言环境?

Is Qt forcing the system locale?

以下示例似乎显示了 Qt 中的一个错误。还是我记错了?

std::cout << atof("0.456") << std::endl; // OK prints 0.456
QApplication app (argc, argv);
//QLocale::setDefault(QLocale::C); // No effect at all.
std::cout << atof("0.456") << std::endl; // do not work on on fr_FR.UTF-8, print 0.

当使用非标准语言环境时,在我的示例中 fr_FR.UTF-8,创建 QApplication 似乎更改了系统语言环境,因为它被 atof 用于进行转换。

在我看来,QApplication 的创建似乎会提取系统语言环境并用它调用 setenv。

To me it looks like the creation of the QApplication will pull the system locale and call a setenv with it.

否,it will call setlocale

void QCoreApplicationPrivate::initLocale()
{
    if (qt_locale_initialized)
        return;
    qt_locale_initialized = true;
#if defined(Q_OS_UNIX) && !defined(QT_BOOTSTRAPPED)
    setlocale(LC_ALL, "");
#endif
}

这反过来从环境变量中获取区域设置,作为空字符串 stands for the user-preferred locale。否则您将使用 C 语言环境。

来自 Qt 文档

Locale Settings

On Unix/Linux Qt is configured to use the system locale settings by default. This can cause a conflict when using POSIX functions, for instance, when converting between data types such as floats and strings, since the notation may differ between locales. To get around this problem, call the POSIX function setlocale(LC_NUMERIC,"C") right after initializing QApplication or QCoreApplication to reset the locale that is used for number formatting to "C"-locale.