宝玛科技网
您的当前位置:首页QT5.2中文乱码解决问题

QT5.2中文乱码解决问题

来源:宝玛科技网


简单的说,从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. #include
  3. #include
  4. #if _MSC_VER >= 1600
  5. #pragma execution_character_set("utf-8")
  6. #endif
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10. QLabel label("???ó??ń??");
  11. label.show();
  12. return a.exec();
  13. }
  14. 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. #include
  6. #include
  7. #include
  8. int main(int argc, char* argv[])
  9. {
  10. QApplication app(argc, argv);
  11. #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  12. #if defined(_MSC_VER) && (_MSC_VER < 1600)
  13. QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030-0"));
  14. #else
  15. QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  16. #endif
  17. #endif
  18. QLabel *label = new QLabel(QObject::tr("你好!"));
  19. label->show();
  20. return app.exec();
  21. }

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

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

显示全文