qt5.3+vs2013亂碼

來源:互聯網
上載者:User

標籤: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/

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.