3

The following example seems to show a bug in Qt. Or am i mistaken ?

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.

When using a non standard locale, in my example fr_FR.UTF-8, creating the QApplication seems to change the system locale, as it is used by atof to do the conversion.

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

Mathieu Westphal
  • 2,544
  • 1
  • 19
  • 33

2 Answers2

7

From Qt documentation

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.

Evgeny
  • 3,910
  • 2
  • 20
  • 37
3

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

No, 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
}

Which in turn fetches the locale from the environment variables, as the empty string stands for the user-preferred locale. Otherwise you'd be using the C locale.

peppe
  • 21,934
  • 4
  • 55
  • 70