今日開始學習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實例之前即可解決;