Solve the problem of QT Chinese garbled and encoding (UTF-8/GBK)

Source: Internet
Author: User

I. QT environment settings

Files from window upload to Ubuntu will display garbled, because the Ubuntu environment is set by default is utf-8,windows default is GBK.
Windows environment, Qt Creator, menu-> tool-> options-> text editor-> behavior-> file encoding:
Default encoding: System (Simplified Chinese Windows system refers to GBK encoding by default, that is, gbk/windows-936-2000/cp936/ms936/windows-936 in dropdown box option)


Second, the coding knowledge Popular Science
Two common types of coding for QT are: UTF-8 and GBK
★utf-8:unicode Transformationformat-8bit allows BOM to be included, but does not usually contain a BOM. is a multi-byte encoding used to solve international characters, which uses 8 bits in English (that is, one byte), and the Chinese uses 24 for (three bytes) to encode. UTF-8 contains the characters that all countries in the world need to use, is international code, strong universality. UTF-8 encoded text can be displayed on browsers that support UTF8 character sets in countries. For example, if the UTF8 code, the foreigner in English IE can also display Chinese, they do not need to download IE Chinese Language support package.
★GBK is the standard of GB2312 compatible GB2312 on the basis of national standard. GBK's text encoding is expressed in two-byte notation, that is, both Chinese and English characters are expressed in double-byte notation, and the highest bits are set to 1 in order to differentiate Chinese. GBK contains all the Chinese characters, is the country code, the universality is worse than the UTF8, but UTF8 occupies the database to be bigger than the GBD. GBK is an extension of GB2312, which, in addition to compatible GB2312, can display traditional Chinese and kana in Japanese.
★GBK, GB2312, and UTF8 must all be converted to each other through Unicode encoding:
GBK, Gb2312--unicode--utf8
UTF8--UNICODE--GBK, GB2312
★ Under the Simplified Chinese Windows system, ANSI encoding represents GBK/GB2312 encoding, and ANSI typically uses a 0x80~0xff range of 2 bytes to represent 1 Chinese characters. The characters between 0x00~0x7f are still 1 bytes representing 1 characters. Unicode (UTF-16) encoding all characters are represented in 2 bytes.


Third, the Code conversion
UTF-8 with ANSI (that is, GBK), you can use the EditPlus tool "file Save as" or the Encodersoft encoding conversion tool to bulk convert the. cpp and. h source file text.


Iv. QT Encoding Designation
QT needs to specify the character encoding used in the main () function:

#include <QTextCodec>

Qtextcodec *codec = Qtextcodec::codecforname ("GBK");//Situation 1

QTEXTCODEC::SETCODECFORTR (codec);
Qtextcodec::setcodecforlocale (codec);
Qtextcodec::setcodecforcstrings (codec);

Or

Qtextcodec *codec = Qtextcodec::codecforname ("UTF-8");//Situation 2
QTEXTCODEC::SETCODECFORTR (codec);
Qtextcodec::setcodecforlocale (codec);
Qtextcodec::setcodecforcstrings (codec);

Here only the most commonly used 3 compilers (Microsoft vs in the Cl,mingw in the G++,linux under the g++), the source code used GBK and no BOM UTF-8 and have a BOM UTF-8 these 3 kinds of coding to save, the phenomenon of the following table shows.

Encoding of source code

Compiler

Show Normal

Display garbled

GBK

Win vs CL

Situation 1

Situation 2

Win mingw-g++

Situation 1

Situation 2

Linux g++

Situation 1

Situation 2

UTF-8 (no BOM)

Win vs CL

Compilation failed

Error C2001: There are line breaks in constants

Compilation failed
Error C2001: There are line breaks in constants

Win mingw-g++

Situation 2

Situation 1

Linux g++

Situation 2

Situation 1

UTF-8 (with BOM)

Win vs CL

Situation 1

Situation 2

Win mingw-g++

Situation 2

Situation 1

Linux g++

Situation 2

Situation 1

V. Application Cases
Qcom Cross-platform serial debugging assistant (http://www.qter.org/?page_id=203)
Source code Qcom\mainwindow.cpp,aboutdialog.cpp and other files are UTF-8 encoded (no BOM), but qcom\qextserial\*.* files are in ANSI encoding. Compile completely OK in Linux environment.
The author of the Windows environment qt creator+ Microsoft vs Compiler, the environment is set up with an ANSI (ie GBK) encoding. Compiling the source file will cause an error.
Error message "Fatal error C1018: Unexpected #elif".


The solution consists of two kinds:

Method 1:


All files in qcom\ are converted to ANSI encoding, and the main () function uses QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GBK")).

Method 2:

The QT Creator environment is set up using the UTF-8 code,


To convert all qcom\ files to Utf-8+bom encoding, note that if the file is converted to UTF-8 (no BOM), the compilation will still fail. The main () function uses QTEXTCODEC::SETCODECFORTR (QTEXTCODEC:: Codecforname ("GBK"))/note that this is still "GBK", not "UTF-8"
Recompile, ok!


Vi. Conclusion

Windows environment, Qt creator+ Microsoft vs Compiler, new project,

1, if the project does not need cross-platform use (only in Win), then the project setup please use the GBK encoding method.

2, if the project to use across the platform (Win+linux), then the project settings, please use the Utf-8+bom coding method.

In Linux environment, Qt CREATOR+GCC, new project,

No GBK encoding is optional, default is UTF-8 (no BOM) encoding, considering cross-platform, we recommend that you choose Utf-8+bom encoding.


Vii. Reference Documents

QT Chinese garbled problem http://blog.csdn.net/brave_heart_lxl/article/details/7186631

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.