参考下这个吧,不知道对你是否有用
1 使用QString的fromLocal8Bit()函数
这个方法是最快的,系统直接自动将char *的参数转换成为系统默认的编码,然后返回一个QString。
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
// QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
// QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
// QLabel hello(QObject::tr("你好"));
// QLabel hello("你好");
// hello.setWindowTitle(QObject::tr("终于搞定中文"));
QString str;
str = str.fromLocal8Bit("哈哈哈");
hello.setWindowTitle(str);
hello.show();
return app.exec();
}
2 用QTextCodec的toUnicode方法来显示中文
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel hello(QObject::tr("你好").toLocal8Bit());
QTextCodec *codec = QTextCodec::codecForLocale();
QString a = codec->toUnicode("安师大手动");
hello.setWindowTitle(a);
hello.show();
return app.exec();
}