Solve the problem of QT Chinese garbled and Chinese character coding (UTF-8/GBK)--ubuntu environment setting default is Utf-8, file encoding can use Encodersoft batch Conversion

Source: Internet
Author: User

First, QT environment settings

Files are garbled when they are uploaded from window to Ubuntu because the Ubuntu environment setting defaults to Utf-8,windows, which is GBK by default.
File encoding for the behavior, such as text editor, options, tools----Qt Creator, menu, Windows environment
Default encoding: System (Simplified Chinese Windows system by default refers to the GBK encoding, which is the dropdown box option in the gbk/windows-936-2000/cp936/ms936/windows-936)

Second, coding knowledge Science
The two common codes for QT are: UTF-8 and GBK
★utf-8:unicode Transformationformat-8bit, which allows BOM, but usually does not include BOM. is a multi-byte encoding used to solve the international character, which uses 8 bits (or one byte) in English, and Chinese uses 24 (three bytes) to encode. UTF-8 contains the characters that all countries in the world need to use, and is an international code with strong versatility. UTF-8 encoded text can be displayed on browsers that support the UTF8 character set in each country. For example, if it is UTF8 code, it can display Chinese in the foreigner's English ie, they do not need to download IE's Chinese language support package.
★GBK is the standard of GB2312 compatible GB2312 on the basis of national standard. The text encoding of the GBK is expressed in double-byte notation, that is, both Chinese and English characters are represented by double-byte, in order to distinguish the language, the highest bit is set to 1. GBK contains all Chinese characters, is the country code, the generality is worse than the UTF8, but UTF8 occupies the database bigger than GBD. GBK is an extension of GB2312, in addition to compatibility with GB2312, it can also display traditional Chinese, as well as Japanese kana.
★GBK, GB2312, and UTF8 must be encoded in Unicode to convert from one to the other:
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 range of 0x80~0xff 2 bytes to represent 1 Chinese characters. The characters between 0x00~0x7f, which are still 1 bytes, represent 1 characters. Unicode (UTF-16) encoding means that all characters are represented in 2 bytes.

Third, the Code conversion
UTF-8 with ANSI (that is, GBK), you can use the EditPlus tool "save file as" or the Encodersoft Encoding conversion tool to batch convert. 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 are only the most commonly used 3 compilers (Microsoft vs in the Cl,mingw in the G++,linux under the g++), the source code using GBK and no BOM UTF-8 and a BOM UTF-8 these 3 kinds of encoding to save, the phenomenon occurs as shown in the following table.

  • Encoding of the source code

    Compiler

    Show Normal

    Garbled display

    GBK

    Win vs CL

    Scenario 1

    Scenario 2

    Win mingw-g++

    Scenario 1

    Scenario 2

    Linux g++

    Scenario 1

    Scenario 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++

    Scenario 2

    Scenario 1

    Linux g++

    Scenario 2

    Scenario 1

    UTF-8 (with BOM)

    Win vs CL

    Scenario 1

    Scenario 2

    Win mingw-g++

    Scenario 2

    Scenario 1

    Linux g++

    Scenario 2

    Scenario 1

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


The solution consists of two types:

Method 1:

Convert all qcom\ files to ANSI encoding using the tool, the main () function uses QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GBK"));

Method 2:

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

Then 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

In 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 settings please use the GBK encoding method.

2, if the project to cross-platform use (win+linux), then the project settings please use the Utf-8+bom encoding method.

Linux environment, Qt CREATOR+GCC, new project,

There is no GBK encoding optional, the default is UTF-8 (no BOM) encoding, considering the cross-platform, it is recommended to choose Utf-8+bom encoding method.

Vii. Reference Documents

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

Solve the problem of QT Chinese garbled and Chinese character coding (UTF-8/GBK)--ubuntu environment setting default is Utf-8, file encoding can use Encodersoft batch Conversion

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.