Qt中文显示问题解决

移动开发
QT默认的编码(unicode)是不能显示中文的,可能由于windows的默认编码的问题,windows默认使用(GBK/GB2312/GB18030),所以需要来更改QT程序的编码来解决中文显示的问题。

Qt中文显示问题的问题,很多编程人员容易头疼的问题,小细节容易忽略,刚刚编写好的程序,运行之后可能会出现不显示或者乱码这种情况,QT默认的编码(unicode)是不能显示中文的,可能由于windows的默认编码的问题,windows默认使用(GBK/GB2312/GB18030),所以需要来更改QT程序的编码来解决中文显示的问题。

QT中有专门的一个类来处理编码的问题(QTextCodec)。

在QT3中,QApplication可以设置程序的默认编码,但是在QT4中已经没有了该成员函数。可以以下的这些方法来设置编码。

1. 设置QObject的成员函数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函数是根据参数中的编码名称,在系统已经安装的编码方案中需找***的匹配编码类型,该查找是大小写不敏感的。如果没有找到,就返回0。

具体的转换代码看下面:

  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后面。不然没有效果。而且这种方法只会转换经过tr函数的字符串,并不转换不经过tr函数的字符串。

技巧:

可以用codecForLocale函数来返回现在系统的默认编码,这样更容易做多编码的程序而不用自己手动来更改具体的编码。

2. 使用QString的fromLocal8Bit()函数

这个方法是最快的,系统直接自动将char *的参数转换成为系统默认的编码,然后返回一个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();  

小结:对于Qt中文显示问题解决,本篇文章介绍完了,这篇文章应该对你很有用!希望能帮到你吧!

【编辑推荐】

浅谈Qt中多线程编程

浅析Qt VC中常用插件

深度解析 QT 编译安装方法

Qt 平台中使GUI保持响应流畅

QT中关于信号与槽机制的实现原理

责任编辑:zhaolei 来源: 互联网
相关推荐

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-08-14 13:49:58

Rails中文问题

2009-06-19 11:16:14

java web中文乱码

2011-04-29 10:18:34

投影机

2011-06-27 16:44:59

Qmake

2009-07-23 16:53:17

ASP.NET中文变问

2011-06-14 13:41:27

muleWSDL

2010-04-28 18:01:15

Unix系统

2010-06-17 11:35:24

Ubuntu 修复Gr

2010-06-09 16:33:46

Cacti中文

2010-05-05 10:25:24

Unix操作系统

2011-11-28 22:45:19

Nginxsession

2009-12-28 10:56:45

WPF Image

2012-05-09 10:08:41

跨机房

2010-05-05 14:20:46

AIX CDE

2011-01-21 14:13:10

2010-07-29 15:28:47

Flex安全沙箱
点赞
收藏

51CTO技术栈公众号