The perfect solution to the problem of Chinese garbled characters in Qt 5

Overview of garbled code problem

When using Qt, we often encounter problems with garbled Chinese characters, garbled qDebug print logs, or garbled Widget interfaces, etc.

The reason is mostly due to the problem of using the MSVC compiler, and the MinGW that comes with Qt generally does not have garbled code problems.

The core reason is because of the encoding problem, that is, the eternal difficult problem of UTF-8 and GBK encoding.

Problem using MSVC compiler to output Chinese

When using the MSVC compiler to compile a project in Qt Creator, garbled Chinese strings may easily occur if not handled properly.
For example, when the program is running, the Chinese characters displayed by LabInfo will be garbled.

void MainWindow::Changed()
{
	QString str="测试";
	ui->LabInfo->setText(str);
} 

This is because the files saved by Qt Creator use UTF-8 encoding (a cross-platform character set that can be used on any platform and any language). Although the MSVC compiler can normally compile UTF-8 encoded source files with BOM , but the encoding of the generated executable file is the Windows local character set, such as GBK2312.

That is to say, in the executable file, the string “测试”is encoded in GBK2312 , but when the executable program executes this statement, the string is decoded in UTF-8 , which will cause garbled characters.

solution

Pure Qt environment

There are two ways to solve this problem

One method is to use QStringLiteral()macros or QString::fromLocal8Bit()encapsulated strings, and the other method is to force the executable file generated by the MSVC compiler to use UTF-8 encoding.

1. Option 1

The QStringLiteral(str) macro generates string data from a string str during compilation, and stores it in the read-only data segment of the compiled file. When the program uses this string when running, it only needs to read out the string data. Can.

Therefore, the QStringLiteral() macro needs to be used in the program to encapsulate each Chinese string . The code can be changed to:

QString str = QStringLiteral("测试");

defect:

Each string containing Chinese needs to be encapsulated, and the tr() function cannot be used (it can be ignored without international translation requirements)

Option II

To force the MSVC compiler to use UTF-8 encoding to generate executable files, you need to add the following statement in front of each header file and source program file that uses Chinese strings:

#if _MSC_VER >= 1600 //VS2015>VS>VS2010, MSVC VER= 10.0 -14.0
#pragma execution_character_set("utf-8")
#endif

The compiler of VS 2010-VS 2015 Update2 can use this scheme, which forces the compiled executable file to be encoded in UTF-8. In this way, even if the QStringLiteral() macro is no longer used, the problem of garbled Chinese characters will no longer occur when the program is running. Moreover, you can also use the tr() function to translate strings.

Personally I think the best solution is:

Add in pro file

msvc{
    QMAKE_CFLAGS += /utf-8
    QMAKE_CXXFLAGS += /utf-8
}

or

msvc{
    QMAKE_CXXFLAGS += /source-charset:utf-8 /execution-charset:utf-8
}

execution-charest represents the execution character set. source-charset represents the source code character set.
Both methods are available.

Note: msvcIt must be in lowercase. For personal testing, uppercase letters MSVCare invalid . In addition, before running, it is recommended to clear it, rebuild it, and delete the previous cache.

VS +Qt environment encoding problem solving

Generally speaking, there will be no garbled characters because the text and compiler are all GBK encoded. But for better cross-platform compatibility, if you change to UTF-8 encoding, you need to make some changes.

  1. First, you need to set the text to UTF-8 format
    , which will not be introduced here. For specific solutions, please refer to VS2019 Encoding Issues, How to Perfectly Change to UTF-8
  2. Add it under Project Properties-C/C++ Command Line-Other Options/utf-8

The test found that using VS2019, the _MSC_VER macro of pure Qt solution 2 can also solve the garbled problem.

Reference link

/utf-8

execution_character_set

Guess you like

Origin blog.csdn.net/no_say_you_know/article/details/121694766