解決qt5.3+vs2013亂碼,在main函數以前加入app
#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環境的判斷):this
// 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也一樣難逃此問題.spa
來自:http://blog.163.com/qimo601@126/blog/static/1582209320143115334438/code