Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Charset problems with Qt 5.* on Windows
naviset last edited by
Need advice now to use Qt 5.* in Visual Studio without setCodecForTr ().
Since Qt 5.0, all string operation must be in utf-8 charset:
"Source code must be UTF-8 and QString wants it":http://www.macieira.org/blog/2012/05/source-code-must-be-utf-8-and-qstring-wants-it/
Visual Studio IDE fully supporting utf-8 source code, all ok, but only until you want to compile them.
When compilation started, compiler convert all strings to current locale codepage:
char *pText="Some text in any language except English";
When you run your programm, even if source code was in utf-8 charset, pointer pText will be in current codepage of your system, in case of Russia, it is cp1251.
If you want to write code like this in your utf-8 source code file:
QString myText("Some text in Russian language encoded utf-8");
It will not work propertly, because QString want utf-8, but pointer will be to text in cp1251.
Visual Studio compiler don't support utf-8 locale even in Visial Studio 2013:
"setlocale in Visual Studio":http://msdn.microsoft.com/en-US/library/x99tb11d.aspx
- If you provide a code page value of UTF-7 or UTF-8, setlocale will fail, returning NULL.*
How to workaround this?
_Andrey_ last edited by
When I used Qt 4.8, I wrote in main.cpp:
QTextCodec::setCodecForTr( QTextCodec::codecForName( "Windows-1251" ) );
and then every string I wrapped in tr() function. And that worked fine.
Now, when I proceed to Qt 5.3 I create a global variable in my project:
QTextCodec *__codec = QTextCodec::codecForName( "Windows-1251" );
Replace every "tr(" or "QObject::tr(" with "__codec->toUnicode(" in the project and make __codec variable avaliable in each file where it is used by
extern QTextCodec *__codec;
This is not a nice way to workaround, but it works well.