解决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/

最新文章

  1. strftime 日期时间格式化
  2. C#对象克隆介绍
  3. atitit.软件开发方法总结O6
  4. MySql 申明变量以及赋值
  5. input:text 的value 和 attribute(&#39;value&#39;) 不是一回事
  6. 特定用户QQ群聊天记录导出的实现
  7. (转)Const,Const函数,Const变量,函数后面的Const
  8. SCSI接口图文详解
  9. oracle 开发笔记“跨数据库查询复制”
  10. [置顶] HMM Tutorial 隐马尔科夫模型
  11. ThinkPHP中处理Layout模板的问题
  12. css3写出飘雪花特效
  13. hadoop解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题
  14. sort();对结构体数组的排序
  15. 设置IE浏览器跨域访问数据
  16. Springboot 的错误处理功能的实现
  17. CSS 图像高级 Css Sprites
  18. Java数据库操作(MySQL与SQLserver)
  19. 便于记忆的SA构造
  20. Spring-boot(二)--thymeleaf

热门文章

  1. Mac 常用属性
  2. 【BZOJ4966】总统选举 线段树+随机化
  3. Tomcat Server 配置
  4. java中随机生成汉字
  5. 九度OJ 1020:最小长方形 (基础题)
  6. virtual dynamic shared object
  7. 利用socket.io实现多人聊天室(基于Nodejs)
  8. windows server 2008 r2 下发布网站excel有时候无法下载文档
  9. bootstrap.min.css.map
  10. 如何识别真Microsoft服务与非Microsoft服务来定位病毒自己的服务