Qtextcodec::makedecoder function, plugins need to be a dynamic link library

Source: Internet
Author: User

The qstring content in QT is encoded as text using Unicode. However, the actual system is usually used in other codes, such as Gbk,utf8. To facilitate compatibility with these formats, two string types are also set in QT:

Qcstring class: C-type string, must end with 0, that is, the middle cannot contain 0. For example, GBK encoded strings

Qbytearray class: Intermediate can contain 0. For example, UTF8 encoded string

To facilitate the transcoding, QT also offers a scalable encoding converter:

Qtextcodec

This class defines the interface of the encoding converter, which can be inherited by plug-ins to implement various encoding converters.

Of course, QT also has a number of encoding converters (not including GBK) built into it.

There are several ways to use the encoding converter:

1. Set the Qobject member function tr () at the beginning of the program, and the string can be converted directly by TR () in the subsequent program;

QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforlocale ()); or Codecforname ("GBK")

... ....

QString Str (TR ("local text"));

2. Use Qtextcodec's Tounicode method to display Chinese

Qtextcodec *codec = Qtextcodec::codecforlocale ();

QString a = Codec->tounicode ("local text");

3. Use Qstring's fromlocal8bit () function family;

QString str = str.fromlocal8bit ("hahaha");

By comparison, Method 1 for the interface text processing is better, can be set at once, global use. and the TR function can be used for internationalization, in the conversion of software to other languages or multi-language version, only through the QM file can be very convenient.

Method 2 is very flexible to use when working with specific coded strings in your program. For example, read the file contents.

Method 3 is only for convenience of the Qstring class, and some encapsulation of the tounicode () function of the Qtextcodec class. The efficiency must be a little worse. For example, Fromlocal8bit () is using the Codeforlocale () decoder.

Problems you may encounter:

Call Qtextcodec::codecforlocale (); no problem.

Instead, call the codecs in plugins, such as GBK. Qtextcodec:: Codecforname ("GBK") can not find the corresponding decoder, get a null pointer.

This is usually the case when your QT compilation is compiled as a static link library. And plugins need to be dynamic link library. To check if the plugins you used has been compiled, the plugins are placed under the qtxxx/plugins/folder. For example, the debug version of gbk,gb18030,gb2312 's decoder is qtxxx/plugins/codecs/qcncodecsd4.dll.

App.addlibrarypath (Qobject::tr ("C://qt-win-opensource-src-4.4.3//plugins")); This code does not need to join the normal execution, if added, the path is best not to have Chinese

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

QString teststr = Codec->tounicode ("GB Code");

In addition, in order to encode some chunked strings, such as string fragments obtained from the network, QT also provides a Qtextdecoder helper class that can help decode such 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);

}

Http://blog.sina.com.cn/s/blog_a401a1ea0101fh3z.html

Qtextcodec::makedecoder function, plugins need to be a dynamic link library

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.