Qt Chinese support record

Source: Internet
Author: User

Chinese garbled problem, record. Maybe you're using this method to achieve culture, but it's really not rigorous . laguage»c++ Title» The most frequently occurring method

#include <QTextCodec>  
int main (int argc, char *argv[]) 
{  
           qapplication app (argc, argv);  
           Set Chinese start
           qtextcodec::setcodecfortr (Qtextcodec::codecforname ("Gb18030″)"); 
          Set Chinese end return
         app.exec (); 
}  



Many places will tell you that this will enable QT to support Chinese, including the tutorials I read, and he even told me to support Chinese must join this code, but I would like to tell you that this approach is problematic, and this problem is not small. I was in two XP system of the same machine on the transplant, there is a Chinese garbled problem, and all Chinese are used tr (). So please don't be superstitious in those tutorials, practice is the truth. You can try this way laguage»c++ Title»

#include <QTextCodec>  
int main (int argc, char *argv[])
{  
        qapplication app (argc, argv);  
           Set Chinese start
           qtextcodec *codec = Qtextcodec::codecforname ("Gb2312″");  
          Qtextcodec::setcodecforlocale (codec);  
           Qtextcodec::setcodecforcstrings (codec);  
          QTEXTCODEC::SETCODECFORTR (codec);  
         Set Chinese end return
           app.exec ();  
}  


Although this method seems more rigorous, you can try, but there will be a problem, at least not solve my problem. This is also my online to see the frequency is also very high method, but I would like to say is: In fact, this solution is not a cure, because it is only to broaden the scope of support for Chinese, and generally we all suggest that the Chinese written in tr (""). recommend a more insurance approach laguage»c++ Title» The method I recommend

int main (int argc, char *argv[]) 
{
    qapplication A (argc, argv);
    Qtextcodec *codec = Qtextcodec::codecforname ("System");    Obtain the system encoding
    Qtextcodec::setcodecforlocale (codec);
    Qtextcodec::setcodecforcstrings (codec);
    QTEXTCODEC::SETCODECFORTR (codec);
    return a.exec ();
}


This gets the system font code, solves my problem, personally also thinks this method should be a good solution. may be improper font selection caused by the garbled

If you always can not solve the garbled problem, you may try to set the font, but in fact, is the font caused by the garbled method is very simple, if the occurrence of a box, that is, improper font settings, otherwise, give priority to coding settings.

Split Line *********************** reprint speech ************************ *************************************

Ps:

    qtextcodec* codec = qtextcodec::codecforname ("UTF8"); If changed to: GB2312, also will appear garbled, as for what reason look forward to everyone answer
    Qtextcodec::setcodecforlocale (codec);
    Qtextcodec::setcodecforcstrings (codec);
    QTEXTCODEC::SETCODECFORTR (codec);
    Qdebug () << "This is Chinese";
    return A.exec ();


Turn from: http://www.ctogo.net/313.html

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.