Qt中中文解碼問題

方法一:

       #include <QApplication>
       #include <QLabel>
       #include <QTextCodec>
int main(int argc, char *argv[]) {
       QApplication app(argc, argv); 
       QTextCodec *codec = QTextCodec::codecForName("gb18030");// Big5-ETen
       QLabel *label = new QLabel; 
       label->setText(codec->toUnicode("<center><h1>Qt4 學習筆記</h1></center>"));   
       label->setWindowTitle(codec->toUnicode("徐忠明"));  
       label->resize(200, 50);   
       label->show();   
       return app.exec();
}

即只要使用codec->toUnicode("中文");就可以實現所以中文的編碼問題,但這個存在一定的缺點,就是每一次都要寫上一個codec->toUnicode();能不能更簡單一點的?

方法二:

QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));//爲tr()這個做設置

這樣就可以使用label->setText(tr("中文"));就可以,而且對項

目中所有的tr()都有效,不必考慮變量的域的問題

如:

QLabel* usrLabel = new QLabel(tr("用戶名:"));

QPushButton* okBtn = new QPushButton(tr("確定"));

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/haha_mingg/archive/2009/08/29/4497623.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章