QApplication and the locale
Tags: programming, software
In Germany, we do things differently. While our way of writing dates is debatable, one thing is a constant source of confusion: The decimal dot. In the US, it’s a real dot–in Germany, it’s a comma. You read that right. π ≈ 3.14159… for scientists, but we continue to claim that it is actually ≈ 3,14159….
Why is this a problem? Well, software sometimes expects input in the form of floating point numbers. In case your locale settings indicate that you are German, smart programs expects you to use the decimal comma, pretty please. Unfortunately, not all parts of our programs are built that smart. In some cases, we bluntly assume that you use decimal dots.
To clear that source of confusion, I added
std::setlocale( LC_NUMERIC, en_US"); at the very
beginning of our program. This fix worked beautifully until that fateful day where stopped. After
much confusion, I found the culprit:
QApplication! It turns out that
QApplication likes to reset
the locale settings for some reason. This is even detailed in the documentation for
QCoreApplication (which I of course read after the problem had already been solved):
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
QCoreApplicationto reset the locale that is used for number formatting to “C”-locale.
So, I heeded the wise words of the fine manual and moved the code for setting the locale after the
QApplication. What a fun way to spend almost one hour.
A colleague suggested that I should search for “
QApplication, what the fuck?“, so these are the
search terms to be used forthwith (the phrase “
QApplication resets locale” might also be helpful,
so I am including this here).