Talking about the Chinese encoding ~__ encoding of QT

Source: Internet
Author: User

Why I can't show my Chinese. In QT use, friends often encounter such problems.
Through Google search, you will find that someone has been resolved, but is to reset the default encoder

Call one of the following two functions first
Qtextcodec *textc=qtextcodec::codecforname ("GBK");
Qtextcodec *textc=qtextcodec::codecforname ("Utf8″");

Then call one of the following 3 functions
Qtextcodec::setcodecforcstrings (TEXTC);
QTEXTCODEC::SETCODECFORTR (TEXTC);
Qtextcodec::setcodecforlocale (TEXTC);

As for the call which is not known, have to try next, anyway, the combination is not much. The problem is solved. But next time, try it again.

Let's take a look at where these functions work.

1 setcodecforcstrings (TEXTC)
This function is used primarily for the default encoding that is used when constructing a Qstring object with a character constants amount or Qbytearray. For example, the following example
*argv[int main (int argc,char]) {
    qapplication app (ARGC,ARGV);

    Qtextcodec *tc=qtextcodec::codecforname ("Utf8″");
    Qtextcodec::setcodecforcstrings (TC);
    QString str ("would rather see two demons in a tug of war than see an angel dancing." ”);
    Qpushbutton ww (str);
    ww.show ();
to
    app.exec ();

If 26, 27 lines are commented out, then what we see on the Qpushbutton will be a bunch of garbled. Because by default qstring the constructor's string parameter as the standard Latin character, it is obvious that garbled characters will appear. In the case of Linux, when you enter in the editor, the default text format is "Utf8″, if you are doing development under Windows, then 26 lines will probably be changed to" GBK.

2.  SETCODECFORTR (TEXTC)
The function is to set the default string encoding when passed to the TR function
int main (int argc,char *argv[]) {
    qapplication app (ARGC,ARGV);

    Qtextcodec *tc=qtextcodec::codecforname ("Utf8″);
    Qtextcodec::setcodecforcstrings (TC);
    QString str (QOBJECT::TR) ("would rather see two demons in a tug of war than see an angel dancing.") ”));
    Qpushbutton ww (str);
    ww.show ();
to
    app.exec ();

The same example, after I added tr at 28 lines, the results of the operation did not display Chinese properly. You need to change the 27-line function to SETCODECFORTR.

3 Qtextcodec::setcodecforlocale (TEXTC)
This function is used primarily for setting and the default encoding format when reading and writing to the local file system. such as the encoding format when content is read through a stream. or the encoding for printing information by Qdebug ().
7 int main (int argc,char *argv[]) {
8     Qtextcodec *tc=qtextcodec::codecforname ("Utf8″);
9     Qtextcodec::setcodecforcstrings (TC);
    QString str ("would rather see two demons in a tug of war than see an angel dancing." ”);
One
    Qtextcodec *tl=qtextcodec::codecforname ("Utf8″);
    Qtextcodec::setcodecforlocale (TL);
    Qdebug () <<str;
15}
You can also try to remove the 12th line or code to "GBK", you will probably understand the role of Setcodecforlocale.

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.