Qt顯示中文的 方法

實際發佈Qt在顯示中文的時候會經常莫名的時好時壞。

在此整理了一些比較可靠的方法與一些備選方法。

1最快的方法:

使用QStringformLocal8bit()

此方法爲:系統直接自動將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.QTextCodectoUnicode方法來顯示中文

#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();


發佈了32 篇原創文章 · 獲贊 14 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章