Recent projects encountered a lot of QT Chinese garbled problem, the main reason is that the customer needs more, QT version useful 4 version of the 5 version is also useful, and there are Windows and Linux cross-platform requirements. Often a problem is solved by Windows, the source code on the Linux compilation does not pass or the Chinese will be garbled, this article is mainly to arrive at a solution to solve the problem of QT Chinese, and support different platforms and different versions. Let's look at the questions in the following scenarios to find the answer:
Scenario 1:
QT Version: qt5.9.4_vs2015
Operating system: WIN10
CPP file encoding: utf8-No BOM format
The CPP section code is as follows:
Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("UTF8"));
QString strmessage = qstring::fromlocal8bit ("I am the UTF8 encoded file:"));
Qdebug () << strmessage;
Try compiling and you will find the compilation error: Error C2001:newline in constant
Why is it. Because UTF8 is divided into utf8-without BOM and Utf8-bom
Utf8-bom is actually more than utf8-no BOM more than a few bytes of the file header, for and UTF-16 and UTF-32 distinguish. Instead: Windows recognized UTF8 refers to Utf-bom (you can use Notepad to save the UTF8 format after viewing).
Because there is a Chinese colon: the existence of the utf8-no BOM file format using VS's CL compiler is not recognized as the UTF8 format, only as ANSI to read parsing and compiling, so compile error. Then someone would say, I'll just change the file format of CPP to: Utf8-bom format. Okay, let's see the next scene.
Scenario 2:
CPP file encoding: Utf8-bom format CPP section code is as follows:
Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("UTF8"));
QString strmessage = qstring::fromlocal8bit ("I am the UTF8 encoded file:"));
Qdebug () << strmessage;
Compile through, run you will find that there is a problem, that is the Chinese garbled, output a bunch of useless information.
What's the problem?
In fact, the Qtextcodec::codecforname ("UTF8") of QT is the result of this code. Note: Qt believes that the UTF8 default is: utf8-no BOM.
Therefore, the UTF8-BOM format of the CPP file is considered to be utf8-no BOM format to parse, it is obvious that the results will be wrong.
Scenario 3:
CPP file encoding: Utf8-bom format
The CPP section code is as follows:
Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("UTF8")); Delete this line
QString strmessage = qstring::fromlocal8bit ("I am the UTF8 encoded file:"));
Qdebug () << strmessage;
or Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("GBK")); Change to GBK encoding
QString strmessage = qstring::fromlocal8bit ("I am the UTF8 encoded file:"));
Qdebug () << strmessage;
Compilation passed, and the output of the Chinese is also correct no garbled.
Scenario 4:
CPP file encoding: GBK encoding
The CPP section code is as follows:
Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("UTF8")); Delete this line
QString strmessage = qstring::fromlocal8bit ("I am the UTF8 encoded file:"));
Qdebug () << strmessage;
or Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("GBK")); Change to GBK encoding
QString strmessage = qstring::fromlocal8bit ("I am the UTF8 encoded file:"));
Qdebug () << strmessage;
Compilation passed, and the output of the Chinese is also correct no garbled.
Summarize:
(1), Chinese encoding all use Qstring::fromlocal8bit () interface.
Cause: The QT4 version needs to be supported. The Qstringliteral () method is also desirable, but it only supports the QT5 version, which can be used if there is no version problem.
(2), CPP and other file encoding all use Utf8-bom format.
Cause 1:utf8-No BOM will fail to compile using the window compiler cl, unless you are compiling the development using QT's MinGW version of Windows.
Cause 2: The file encoding that does not use GBK is for easier compatibility to Linux versions with Windows, otherwise you need to change the Qtcreate encoding environment with the Linux system character environment.
(3), if you do not use the method (1) in the corresponding. h file or. cpp file at the top of the above add the following definition, and must meet the requirements of (2). If you just want to define it again, add the following code using a precompiled header. (The one I researched)
#if defined (_msc_ver) && (_msc_ver >= 1600)
#pragma execution_character_set ("Utf-8")
#endif
Other knowledge: QString Str (QOBJECT::TR ("Chinese")) can be used.
Answer: No, tr () can only be in English.
Because the QT5 version cancels the QTEXTCODEC::SETCODECFORTR () method. And you have to understand what QOBJECT::TR is doing. It is used for the internationalization of the program, you can also translate the interface text into different languages. If you use QOBJECT::TR, you should be all in English, and then with the help of linguist translated into Chinese, it will not garbled. For more information, please search for "qt internationalization".