Heim  >  Artikel  >  Datenbank  >  QT5.2中文乱码解决问题

QT5.2中文乱码解决问题

WBOY
WBOYOriginal
2016-06-07 15:43:041555Durchsuche

总结: 之前Qt4写的代码用QTextCodec::codecForTr()来解决中文输入问题,为了防止错误的滥用,在Qt5中已经取消了这个方法。 Qt5VS2010 中文乱码问题的解决:【请记住VS2010必须打普定vs2010sp1,下面代码才有效】 #if _MSC_VER = 1600 #pragma execution_cha

 总结:之前Qt4写的代码用QTextCodec::codecForTr()来解决中文输入问题,为了防止错误的滥用,在Qt5中已经取消了这个方法。

          Qt5+VS2010中文乱码问题的解决:【请记住VS2010必须打普定vs2010sp1,下面代码才有效】

<p><span><span>#if _MSC_VER >= 1600</span>
<span>#pragma</span><span> execution_character_set</span><span>(</span><span>"utf-8"</span><span>)</span>
<span>#endif</span></span></p>


Qt+Visualstudio的中文乱码总结:

1、Qt4.7+VS2008,通过如下方法:

  1. #include   
  2. int main(int argc, char *argv[])  
  3. {  
  4.     QApplication a(argc, argv);  
  5.   
  6.     // 以下部分解决中文乱码  
  7.     QTextCodec *codec = QTextCodec::codecForName("GBK");  
  8.     QTextCodec::setCodecForTr(codec);  
  9.     QTextCodec::setCodecForLocale(codec);  
  10.     QTextCodec::setCodecForCStrings(codec);  
    }

 

2、Qt5.2+VS2010打入补丁VS2010sp1,也会支持 #pragma execution_character_set ("utf-8"),顺利解决qt中文乱码问题

          在main函数之前加入:

  1. #if _MSC_VER >= 1600  
  2. #pragma execution_character_set("utf-8")  
  3. #endif  

 

3、Qt5.2+ VS2012 不支持 #pragma execution_character_set ("utf-8") 导致utf-8编码的无法直接输出中文,qt首当其冲受害。 微软官方回应,再下一个版本中会修正这个问题:

          由 Microsoft 在 2013/1/3 於 10:24 公佈

Hi Seek:
    Thanks for reporting the issue.
    A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.

Xiang Fan
Visual C++ Team

4、Qt5.2+VS2013支持 #pragma execution_character_set ("utf-8"),所以qt也赶紧发布了一个

         Visual Studio Add-in 1.2.3 Alpha for Qt5 (supports versions 2008, 2010, 2012 and 2013, ess edition)

          在main函数之前加入:

  1. #if _MSC_VER >= 1600  
  2. #pragma execution_character_set("utf-8")  
  3. #endif  


5、回到 Qt5 的中文输出问题。

 Qt默认支持 VS2010/MinGW/Gcc 等编译器,而它们现在都已经真正支持UTF8了。当然,VS2010 对UTF8的支持会入侵代码(#pragma execution_character_set("utf-8"))。 看看Qt官方论坛别人是怎么说的:

http://qt-project.org/forums/viewthread/17617

Nothing special need to do, it will works by default. 
If the exec-charset of your your compiler is UTF-8.

       简单的说,从Qt5开始,源代码就是默认UTF8编码的。

       当然,VC2010编辑器对带BOM的UTF8也是认识,只可惜VC2010编译器根本承认它是UTF8!

       在继续看官方论坛的回复:

You can write a simple example like this

  1. You can write a simple example like this  
  2.   
  3. #include   
  4. #include   
  5.    
  6.   #if _MSC_VER >= 1600  
  7.   #pragma execution_character_set("utf-8")  
  8.   #endif  
  9.    
  10.   int main(int argc, char *argv[])  
  11.   {  
  12.       QApplication a(argc, argv);  
  13.       QLabel label("???ó??ń??");  
  14.       label.show();  
  15.    
  16.       return a.exec();  
  17.   }  
  18. If other people can reproduce your problem, you can file a bug.  

If other people can reproduce your problem, you can file a bug.

       较完整的解决方案(增加了Qt4/Qt5和非VC环境的判断):

  1. // Coding: UTF-8(BOM)  
  2. #if defined(_MSC_VER) && (_MSC_VER >= 1600)  
  3. # pragma execution_character_set("utf-8")  
  4. #endif  
  5.    
  6. #include   
  7. #include   
  8. #include   
  9.    
  10. int main(int argc, char* argv[])  
  11. {  
  12.     QApplication app(argc, argv);  
  13.    
  14.    #if QT_VERSION   
  15.         #if defined(_MSC_VER) && (_MSC_VER   
  16.                 QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030-0"));  
  17.         #else  
  18.                 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));  
  19.         #endif  
  20.    #endif  
  21.    
  22.     QLabel *label = new QLabel(QObject::tr("你好!"));  
  23.     label->show();  
  24.   
  25.     return app.exec();  
  26. }  

 另外:Qt4/Qt5/Linux: 只要是默认的UTF8环境, 应该都没问题

其实这个问题不是Qt特有的, 追根溯源还是C/C++和编译器的问题.即使是支持UTF16的Java也同样难逃此问题。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:TMSC64XX DSP混合汇编2Nächster Artikel:CCI 2.5 链表整数求和