Qt Windows/Linux supports Chinese Encoding

Source: Internet
Author: User

In QT, Chinese characters are often used, whether it is a string or a path name. Fortunately, QT provides the qtextcodec class, which has a powerful encoding format conversion function and provides most common character encoding formats in the world.

In QT, the string using qstring adopts unicode encoding. in windows, we use GBK encoding (a Chinese encoding). in Linux, UTF-8 encoding is usually used.

1. Use Chinese Characters in Windows

Add the following in the main () function:

1 # include <qtextcodec> 2 3 qtextcodec: setcodecforlocale (qtextcodec: codecforname ("GBK"); // path name supports Chinese characters 4 qtextcodec: setcodecfortr (qtextcodec :: codecforname ("GBK"); // qstring supports Chinese 5 qtextcodec: setcodecforcstrings (qtextcodec: codecforname ("GBK"); // string supports Chinese

2. in Linux

Replace the above "GBK" with "UTF-8 ".

3. Other cases

Sometimes, even if you add a file above, garbled characters still occur when you use Chinese Characters in other files. (if you do not mention it, I have encountered such problems ...). At this time, there are other solutions

For example, if tr ("Chinese") of qstring type is displayed, the result of "Chinese" is garbled, and TR ("Chinese") is replaced

Qtextcodec: codecforname ("GBK")-> tounicode ("Chinese ")

In this way, Chinese characters are displayed normally. In fact, this is also a solution to prevent Chinese garbled characters, but in this case, every place with a qstring of Chinese characters must be modified in this way, which cannot be solved, who told me to add those three sentences or garbled characters.

4. digress

Of course, QT provides translation for different languages ,. However, the program we write is only used in China, so there is no need for such high-end features. It is written in Chinese in the program.

Related Article

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.