简单的说,从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 < 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也同样难逃此问题。 
下载本文