Qt 中文亂碼解決大全

來源:互聯網
上載者:User

標籤:blog   http   strong   檔案   io   art   

源地址:http://blog.csdn.net/xcy2011sky/article/details/7168376

解決中文亂碼,最好知道亂碼是什麼格式比如說:utf-8.

解決方案:

1.讓整個應用程式支援中文:

  在  QApplication a(argc, argv);後加上以下幾句話即可。要包含標頭檔#include <QTextCodec>。

 QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));

2.本地中文上傳到WebService,要把中文編碼成utf8格式。這樣才不至於產生亂碼!

例如:QString str="中文測試";

const char* result=str.toUtf8();

 

3.從網路上擷取utf8編碼的中文,在用戶端應用程式顯示中文。

例如:

QString str=slist[i].u_nickname;
   item1->setText(QString::fromUtf8(str.toLocal8Bit()));

4.從網路上下載含有中文的xml時,下載完成打不開。

例如:

 QTextStream out(&file);
   out.setCodec("utf-8");
   QTextCodec *tc=QTextCodec::codecForName("utf-8");
   QByteArray ba=reply->readAll();
   QString str=tc->toUnicode(ba);
   out<<str;
   reply->deleteLater();

 

5.QT4發布應用程式時解決亂碼:

在部署目錄(執行程式放置的目錄)中建立子目錄 plugins,之後在此目錄中建立codecs子目錄,將qcncodecs4.dll複製到此子目錄中,形成如下目錄結構:

<app-install-dir>\plugins\codecs

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.