標籤:style blog http color java io for 2014
解決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/[email protected]/blog/static/1582209320143115334438/