实际发布Qt在显示中文的时候会经常莫名的时好时坏。
在此整理了一些比较可靠的方法与一些备选方法。
1最快的方法:
使用QString的formLocal8bit()
此方法为:系统直接自动将char * 的参数转换成为系统默认的编码,然后返回一个QString。
#include <QTextCodec>
……
{
……
QString str;
str = str.fromLocal8Bit("Qt中文显示");
hello.setWindowTitle(str);
……
}
2我使用的方法:
(发布的时候不需要包含qcncodecs4.dll)
#include <QTextCodec>
int main(int argc, char *argv[])
{
……
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
……
}
另外还有下面几种方法可以试试:
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));
3.用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("Qt中文显示");
hello.setWindowTitle(a);
hello.show();
return app.exec();
}