Qt5的中文亂碼問題如何解決

今日開始學習QT編程,本人一開始用QT5.2,剛寫了一個Helloworld程序,顯示中文時候出現了錯誤:

#include <QApplication>
#include <QDialog>
#include <QTextCodec>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    QDialog w;
    QLabel label(&w);
    label.setText(QObject::tr("Hell World!你好 QT!"));
    w.show();
    return a.exec();
}


原因:

Qt5 取消了QTextCodec::setCodecForTr()和QTextCodec::setCodecForCString()這兩個函數。

解決方法:

直接將 QTextCodec::setCodecForLocale(QTextCodec::codecForName(“UTF8”)) 至於QApplication實例之前即可解決;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章