Perfectly solve the problem of QT Chinese garbled characters

reason

It can be summed up in one sentence: the encoding of the source code character set and the execution character set are not uniform ! That is, the code code edited in the IDE and the generated running program code are not uniform, and garbled characters will inevitably appear.

necessary knowledge

This article mainly provides solutions, and the principle is necessary to be solved. After careful study, the understanding will be more thorough. Understand the basic principles of garbled codes, locate and solve garbled codes in minutes in the future, there are many related articles, search by yourself! Around these nouns: character encoding, character set, such as ASCII, UNICODE, GBK, GB2312, GB18030, UTF-8, UTF-16, etc., source code character set, execution character set, MSVC compiler, MinGW compiler, etc., are almost enough up.

Analysis and Methods

There are mainly two types of Qt program development: QtCreator and Qt+VS. The garbled characters are mainly the scene of using the MSVC compiler.
The default QtCreator + Mingw compiler generally does not have garbled characters ;
while the QtCreator + MSVC compiler is prone to garbled characters . The reason is that the QtCreator source character set defaults to UTF8, and the MSVC execution character set defaults to GBK. Appear.
Personally, I often use native QtCreator for development. The Qt+VS environment is very friendly to debugging. I don’t know much about others, but there should be no garbled characters, because the source code character set and execution character set are both GBK, so there is no problem in unification.

The method is ready to come out, just two steps:

  1. Specify the source character set in the IDE
    set encoding in options
  2. Specify the execution character set in the pro file of the project
    pro set the execution character set

If there is a problem with garbled characters in the VS environment, you can also check the relevant settings and modify them yourself.
On the premise that the above settings are correct, note that there may still be garbled characters in one case: some information returned by third-party SDKs such as Hikvision shows garbled characters . The reason is that the encoding returned by the SDK is inconsistent, just convert it with QString::fromLocal8Bit.

In addition, consider that the Linux environment defaults to UTF encoding. If it is cross-platform, it is best to unify it as UTF8. The above method ideas should be enough to solve common garbled code problems in work, and other problems can only be analyzed in specific scenarios. Please correct me if there is any mistake.

Guess you like

Origin blog.csdn.net/T__zxt/article/details/126469296