Qt Chinese garbled Problem Solution

Source: Internet
Author: User

Maybe you are using this method to implement culture, but it is not rigorous enough. Laguage» C ++ Title» Methods with the highest frequency
  1. # Include <qtextcodec>
  2. Int main (INT argc, char * argv [])
  3. {
  4. Qapplication app (argc, argv );
  5. // Set Chinese to start
  6. Qtextcodec: setcodecfortr (qtextcodec: codecforname ("gb18030 "));
  7. // Set Chinese end
  8. Return app.exe C ();
  9. }


Many places will tell you that QT supports Chinese characters, including the tutorials I read. He even told me that this code must be added to support Chinese characters, but I want to tell you, there is a problem with this method, and this problem is not small! When I port two machines that are also Windows XP, Chinese garbled characters occur, and TR () is used for all Chinese characters. So don't be superstitious about those tutorials. Practice is the truth!

You can try this method Laguage» C ++ Title»
  1. # Include <qtextcodec>
  2. Int main (INT argc, char * argv [])
  3. {
  4. Qapplication app (argc, argv );
  5. // Set Chinese to start
  6. Qtextcodec * codec = qtextcodec: codecforname ("gb2312 ");
  7. Qtextcodec: setcodecforlocale (codec );
  8. Qtextcodec: setcodecforcstrings (codec );
  9. Qtextcodec: setcodecfortr (codec );
  10. // Set Chinese end
  11. Return app.exe C ();
  12. }

Although this method seems more rigorous, you can try it. However, there will still be problems in this writing, at least it will not solve my problem. This is also a high frequency method I have seen on the Internet, but I want to say: in fact, this method is not a cure, because it is only to broaden the scope of Chinese support, generally, we recommend that you write Chinese Characters in tr.

Recommend a safer method Laguage» C ++ Title» My recommended methods
  1. Int main (INT argc, char * argv [])
  2. {
  3. Qapplication A (argc, argv );
  4. Qtextcodec * codec = qtextcodec: codecforname ("system"); // obtain the system code
  5. Qtextcodec: setcodecforlocale (codec );
  6. Qtextcodec: setcodecforcstrings (codec );
  7. Qtextcodec: setcodecfortr (codec );
  8. Return a.exe C ();
  9. }

I have obtained the system font code and solved my problem. I personally think this solution should be a good solution.

It may also be garbled characters caused by improper font selection.

If you cannot solve the Garbled text problem all the time, you can try to set the font, but whether the font causes Garbled text is very simple. If there are blocks, the font is improperly set. Otherwise, encoding settings are preferred.

 

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.