Qt5的中文乱码问题如何解决

来源:互联网 发布:ae2015 mac破解补丁 编辑:程序博客网 时间:2024/04/28 15:05

今日开始学习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实例之前即可解决;


0 0
原创粉丝点击