Qt text encoding conversion method

Source: Internet
Author: User

Http://hi.baidu.com/wenjiejia/blog/item/cf7fd2b44194237b8bd4b2e3.html

 

The qstring content in QT uses Unicode as text encoding. However, other codes, such as GBK and utf8, are usually used in the actual system. To facilitate compatibility with these formats, QT also sets two string types:

Qcstring: a string of the C type, which must end with 0, that is, the string cannot contain 0. For example, a string encoded by GBK.

Qbytearray class: Contains 0 strings, such as UTF-8 encoded strings.

 

To facilitate encoding conversion, QT also provides a set of scalable encoding converters:

Qtextcodec

This class defines the encoding converter interface, which can be inherited as a plug-in to implement various encoding converters.

Of course, QT also has multiple built-in encoding converters (excluding GBK ).

When using an encoding converter, you can use the following methods:

 

1. Set the encoder of the member function tr () of qobject at the beginning of the program. In subsequent programs, TR () can be used to directly convert strings;

Qtextcodec: setcodecfortr (qtextcodec: codecforlocale (); // or codecforname ("GBK ")

... ....

Qstring STR (TR ("Local text "));

2. Use the tounicode method of qtextcodec to display Chinese Characters

Qtextcodec * codec = qtextcodec: codecforlocale ();

Qstring A = codec-> tounicode ("Local text ");

3. Use the fromlocal8bit () function family of qstring;

Qstring STR = Str. fromlocal8bit ("HAHAHA ");

 

Through comparison, method 1 is better for interface text processing and can be set at a time for global use. Moreover, the tr function can be used for internationalization. You only need to use the Qm file to convert the software to other language or multi-language versions.

Method 2 is flexible and suitable for processing specific encoding strings in a program, for example, reading file content.

Method 3 encapsulates the tounicode () function of the qtextcodec class to facilitate the use of the qstring class. Efficiency must be lower. For example, fromlocal8bit () uses the codeforlocale () decoder.

 

Possible problems:

Call qtextcodec: codecforlocale (); no problem.

Call codecs in plugins, for example, GBK. qtextcodec: codecforname ("GBK"). The corresponding decoder cannot be found and a null pointer is obtained.

This usually occurs when your QT is compiled as a static Link Library. The plugins must be a dynamic link library. Check whether the Plugins you are using have been compiled. The INS are all placed in the qtxxx/plugins/folder. For example, the debug version of GBK, gb18030, and gb2312 decoder is qtxxx/plugins/codecs/qcncodecsd4.dll.

 

// App. addlibrarypath (qobject: TR ("C: // qt-win-opensource-src-4.4.3 // plugins"); // This code can be executed normally without being added, it is best not to have Chinese characters in the path

Qtextcodec * codec = qtextcodec: codecforname ("GBK ");

Qstring teststr = codec-> tounicode ("GB encoding ");

STD: String stdstring ("Chinese ");

Qstring strchinese = codec-> tounicode (stdstring. c_str ());

 

 

In addition, to encode and convert some segmented strings (such as string fragments obtained from the Network), QT also provides a qtextdecoder helper class to help decode these strings.

Qtextcodec * codec = qtextcodec: codecforname ("shift-JIS ");

Qtextdecoder * decoder = codec-> makedecoder ();

 

Qstring string;

While (new_data_available ()){

Qbytearray chunk = get_new_data ();

String + = decoder-> tounicode (chunk );

}

 

 

 

Convert Chinese characters to STD: String

Qtextcodec * codec = qtextcodec: codecforname ("GBK ");
If (codec)
{

Qstring strchinese ("China ");
Qbytearray gbkstring = codec-> fromunicode (strchinese );
STD: String strstd = gbkstring. Data ();
}

 

OSG displays Chinese Characters

Osgtext: Text * ptext;

Qstring strunicodetext ("China ");
Qbytearray utf8bytearray = strunicodetext. toutf8 ();
Ptext-> settext (utf8bytearray. Data (), osgtext: String: encoding_utf8 );

 

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.