Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1819865
  • 博文数量: 496
  • 博客积分: 12043
  • 博客等级: 上将
  • 技术积分: 4778
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-27 14:26
文章分类

全部博文(496)

文章存档

2014年(8)

2013年(4)

2012年(181)

2011年(303)

2010年(3)

分类: C/C++

2011-11-24 13:54:51

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

AD:

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显示中文乱码问题

贴代码 先
#include
#include
#include
#include
#include



#include
int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QWidget* pWidget = new QWidget;
QLabel label(pWidget);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));
label.setText(QObject::tr("同一个世界,同一个梦想"));
QPushButton* btn = new QPushButton(QObject::tr("关闭"), pWidget);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(&label);
layout->addWidget(btn);
pWidget->setLayout(layout);
QObject::connect(btn, SIGNAL(clicked()), pWidget, SLOT(close()));
pWidget->show();
return app.exec();
}

上面运行乱码
解决办法:
第一 将文件保存成utf8编码格式 如果是用creator ,则选择edit菜单的最后一个子菜单
第二 GB18030 用utf8 替换


最近在学习使用QT,但是过程中碰到了中文乱码的问题,在此将我找到的解决方法共享一下:

1.界面的中文乱码

      当我们直接使用以下语句往树组件添加节点时,在显示的时候会出现乱码

      QStandardItem *item = new QStandardItem(QString("例子"));

     我们可以在主窗口初始化的时候调用以下语句:

//set codec

    QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());

    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));

 

    我们界面就能正常显示中文了。

2.SQLITE数据库数据取出并显示时出现的乱码

    一开始我往数据库添加中文数据的时候是用了QString::fromUtf8("例子")进行描述的,但是取出来的数据在显示时还是乱码(我已经添加了 第一步中的 //set codec 语句块)。后来发现其实也只要使用QString("例子")就可以了,不用担心编码的问题。

阅读(1337) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~