qt5.3+vs2013乱码

解决qt5.3+vs2013乱码,在main函数之前加入

#if _MSC_VER >= 1600  
#pragma execution_character_set("utf-8")  
#endif 

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

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

//You can write a simple example like this  

  
#include <QApplication>  
#include <QLabel>  
   
  #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.  

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

// Coding: UTF-8(BOM)  
#if defined(_MSC_VER) && (_MSC_VER >= 1600)  
# pragma execution_character_set("utf-8")  
#endif  
   
#include <QApplication>  
#include <QTextCodec>  
#include <QLabel>  
   
int main(int argc, char* argv[])  
{  
    QApplication app(argc, argv);  
   
   #if QT_VERSION < QT_VERSION_CHECK(5,0,0)  
        #if defined(_MSC_VER) && (_MSC_VER < 1600)  
                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也同样难逃此问题.

来自:http://blog.163.com/qimo601@126/blog/static/1582209320143115334438/

原文地址:https://www.cnblogs.com/lovelp/p/3878416.html