Qt 4 and iconv(3) mini-HOWTO

Qt 4 since 4.2.0 uses iconv(3) for QTextCodec::codecForLocale() unless thou disablest it with -no-iconv configure option. This leadeth to the two things: name() for this codec returneth “System” and QTextCodec::codecForLocale() just stoppeth working. Try to encode or decode something outside of US-ASCII with it, and thou wilt get either empty string or just US-ASCII characters from it, if there were any.

Here is the fix:

#include <locale.h>
setlocale(LC_ALL, “”) // this goeth to thy main()

The nature of the problem is described in the glibc manual:

A C program inherits its locale environment variables when it starts up.
This happens automatically. However, these variables do not
automatically control the locale used by the library functions, because
ISO C says that all programs start by default in the standard C'
locale. To use the locales specified by the environment, you must call
setlocale’. Call it as follows:

setlocale (LC_ALL, “”);

Leave a Reply