您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

QT5.2中文乱码解决问题

2025/11/16 7:44:12发布36次查看
总结: 之前qt4写的代码用qtextcodec::codecfortr()来解决中文输入问题,为了防止错误的滥用,在qt5中已经取消了这个方法。 qt5vs2010 中文乱码问题的解决:【请记住vs2010必须打普定vs2010sp1,下面代码才有效】 #if _msc_ver = 1600 #pragma execution_cha
 总结:之前qt4写的代码用qtextcodec::codecfortr()来解决中文输入问题,为了防止错误的滥用,在qt5中已经取消了这个方法。
          qt5+vs2010中文乱码问题的解决:【请记住vs2010必须打普定vs2010sp1,下面代码才有效】
#if _msc_ver >= 1600#pragma execution_character_set(utf-8)#endif

qt+visualstudio的中文乱码总结:
1、qt4.7+vs2008,通过如下方法:
#include   int main(int argc, char *argv[])  {      qapplication a(argc, argv);        // 以下部分解决中文乱码      qtextcodec *codec = qtextcodec::codecforname(gbk);      qtextcodec::setcodecfortr(codec);      qtextcodec::setcodecforlocale(codec);      qtextcodec::setcodecforcstrings(codec);  

2、qt5.2+vs2010打入补丁vs2010sp1,也会支持 #pragma execution_character_set (utf-8),顺利解决qt中文乱码问题
          在main函数之前加入:
#if _msc_ver >= 1600  #pragma execution_character_set(utf-8)  #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函数之前加入:
#if _msc_ver >= 1600  #pragma execution_character_set(utf-8)  #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
you can write a simple example like this    #include   #include        #if _msc_ver >= 1600    #pragma execution_character_set(utf-8)    #endif       int main(int argc, char *argv[])    {        qapplication a(argc, argv);        qlabel label(?óń);        label.show();           return a.exec();    }  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环境的判断):
// coding: utf-8(bom)  #if defined(_msc_ver) && (_msc_ver >= 1600)  # pragma execution_character_set(utf-8)  #endif     #include   #include   #include      int main(int argc, char* argv[])  {      qapplication app(argc, argv);        #if qt_version           #if defined(_msc_ver) && (_msc_ver                   qtextcodec::setcodecfortr(qtextcodec::codecforname(gb18030-0));          #else                  qtextcodec::setcodecfortr(qtextcodec::codecforname(utf-8));          #endif     #endif         qlabel *label = new qlabel(qobject::tr(你好!));      label->show();        return app.exec();  }   另外:qt4/qt5/linux: 只要是默认的utf8环境, 应该都没问题
其实这个问题不是qt特有的, 追根溯源还是c/c++和编译器的问题.即使是支持utf16的java也同样难逃此问题。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product