自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Qt中文顯示問題解決

移動開發(fā)
QT默認(rèn)的編碼(unicode)是不能顯示中文的,可能由于windows的默認(rèn)編碼的問題,windows默認(rèn)使用(GBK/GB2312/GB18030),所以需要來更改QT程序的編碼來解決中文顯示的問題。

Qt中文顯示問題的問題,很多編程人員容易頭疼的問題,小細(xì)節(jié)容易忽略,剛剛編寫好的程序,運行之后可能會出現(xiàn)不顯示或者亂碼這種情況,QT默認(rèn)的編碼(unicode)是不能顯示中文的,可能由于windows的默認(rèn)編碼的問題,windows默認(rèn)使用(GBK/GB2312/GB18030),所以需要來更改QT程序的編碼來解決中文顯示的問題。

QT中有專門的一個類來處理編碼的問題(QTextCodec)。

在QT3中,QApplication可以設(shè)置程序的默認(rèn)編碼,但是在QT4中已經(jīng)沒有了該成員函數(shù)??梢砸韵碌倪@些方法來設(shè)置編碼。

1. 設(shè)置QObject的成員函數(shù)tr()的編碼。

 

  1. QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));  
  2. Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive.   
  3. Returns 0 if no codec matching the name name could be found. 

其中的codecForName函數(shù)是根據(jù)參數(shù)中的編碼名稱,在系統(tǒng)已經(jīng)安裝的編碼方案中需找***的匹配編碼類型,該查找是大小寫不敏感的。如果沒有找到,就返回0。

具體的轉(zhuǎn)換代碼看下面:

  1. #include <QApplication> 
  2. #include <QTextCodec> 
  3. #include <QLabel> 
  4. int main(int argc,char *argv[])  
  5. {  
  6.    QApplication app(argc,argv);  
  7.    QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));  
  8.    QLabel hello(QObject::tr("你好"));  
  9.    hello.setWindowTitle(QObject::tr("終于搞定中文"));  
  10.    hello.show();  
  11.    return app.exec();  

注意:

setCodecForTr一定要在QApplication后面。不然沒有效果。而且這種方法只會轉(zhuǎn)換經(jīng)過tr函數(shù)的字符串,并不轉(zhuǎn)換不經(jīng)過tr函數(shù)的字符串。

技巧:

可以用codecForLocale函數(shù)來返回現(xiàn)在系統(tǒng)的默認(rèn)編碼,這樣更容易做多編碼的程序而不用自己手動來更改具體的編碼。

2. 使用QString的fromLocal8Bit()函數(shù)

這個方法是最快的,系統(tǒng)直接自動將char *的參數(shù)轉(zhuǎn)換成為系統(tǒng)默認(rèn)的編碼,然后返回一個QString。

  1. #include <QApplication> 
  2. #include <QTextCodec> 
  3. #include <QLabel> 
  4. int main(int argc,char *argv[])  
  5. {  
  6.    QApplication app(argc,argv);  
  7. // QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));  
  8. // QTextCodec::setCodecForTr(QTextCodec::codecForLocale());  
  9. // QLabel hello(QObject::tr("你好"));  
  10. // QLabel hello("你好");  
  11. // hello.setWindowTitle(QObject::tr("終于搞定中文"));  
  12.    QString str;  
  13.    strstr = str.fromLocal8Bit("哈哈哈");  
  14.    hello.setWindowTitle(str);  
  15.    hello.show();  
  16.    return app.exec();  

3. 用QTextCodec的toUnicode方法來顯示中文

  1. #include <QApplication> 
  2. #include <QTextCodec> 
  3. #include <QLabel> 
  4. int main(int argc,char *argv[])  
  5. {  
  6.    QApplication app(argc,argv);  
  7.    QLabel hello(QObject::tr("你好").toLocal8Bit());  
  8.    QTextCodec *codec = QTextCodec::codecForLocale();  
  9.    QString a = codec->toUnicode("安師大手動");  
  10.    hello.setWindowTitle(a);  
  11. hello.show();  
  12. return app.exec();  

小結(jié):對于Qt中文顯示問題解決,本篇文章介紹完了,這篇文章應(yīng)該對你很有用!希望能幫到你吧!

【編輯推薦】

淺談Qt中多線程編程

淺析Qt VC中常用插件

深度解析 QT 編譯安裝方法

Qt 平臺中使GUI保持響應(yīng)流暢

QT中關(guān)于信號與槽機(jī)制的實現(xiàn)原理

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-03-18 18:47:34

QtMySQL

2009-07-17 14:33:05

Jython中文問題

2009-06-09 15:51:07

Java ee中文問題解決方法

2009-02-18 14:28:23

編碼亂碼JSP

2009-06-19 11:16:14

java web中文亂碼

2009-08-14 13:49:58

Rails中文問題

2011-04-29 10:18:34

投影機(jī)

2011-06-27 16:44:59

Qmake

2009-07-23 16:53:17

ASP.NET中文變問

2011-06-14 13:41:27

muleWSDL

2010-06-17 11:35:24

Ubuntu 修復(fù)Gr

2010-04-28 18:01:15

Unix系統(tǒng)

2010-06-09 16:33:46

Cacti中文

2011-11-28 22:45:19

Nginxsession

2009-12-28 10:56:45

WPF Image

2010-05-05 10:25:24

Unix操作系統(tǒng)

2012-05-09 10:08:41

跨機(jī)房

2010-05-05 14:20:46

AIX CDE

2011-01-21 14:13:10

2009-06-30 14:02:00

Struts亂碼Eclipse
點贊
收藏

51CTO技術(shù)棧公眾號