Qt中文显示问题解决
2015-01-14 11:54
423 查看
QT默认的编码(unicode)是不能显示中文的,可能由于windows的默认编码的问题,windows默认使用(GBK/GB2312/GB18030),所以需要来更改QT程序的编码来解决中文显示的问题。
AD:WOT2015 互联网运维与开发者大会 热销抢票
Qt中文显示问题的问题,很多编程人员容易头疼的问题,小细节容易忽略,刚刚编写好的程序,运行之后可能会出现不显示或者乱码这种情况,QT默认的编码(unicode)是不能显示中文的,可能由于windows的默认编码的问题,windows默认使用(GBK/GB2312/GB18030),所以需要来更改QT程序的编码来解决中文显示的问题。
QT中有专门的一个类来处理编码的问题(QTextCodec)。
在QT3中,QApplication可以设置程序的默认编码,但是在QT4中已经没有了该成员函数。可以以下的这些方法来设置编码。
1. 设置QObject的成员函数tr()的编码。
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive.
Returns 0 if no codec matching the name name could be found.
其中的codecForName函数是根据参数中的编码名称,在系统已经安装的编码方案中需找最佳的匹配编码类型,该查找是大小写不敏感的。如果没有找到,就返回0。
具体的转换代码看下面:
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QLabel hello(QObject::tr("你好"));
hello.setWindowTitle(QObject::tr("终于搞定中文"));
hello.show();
return app.exec();
}
注意:
setCodecForTr一定要在QApplication后面。不然没有效果。而且这种方法只会转换经过tr函数的字符串,并不转换不经过tr函数的字符串。
技巧:
可以用codecForLocale函数来返回现在系统的默认编码,这样更容易做多编码的程序而不用自己手动来更改具体的编码。
2. 使用QString的fromLocal8Bit()函数
这个方法是最快的,系统直接自动将char *的参数转换成为系统默认的编码,然后返回一个QString。
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
// QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
// QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
// QLabel hello(QObject::tr("你好"));
// QLabel hello("你好");
// hello.setWindowTitle(QObject::tr("终于搞定中文"));
QString str;
strstr = str.fromLocal8Bit("哈哈哈");
hello.setWindowTitle(str);
hello.show();
return app.exec();
}
3. 用QTextCodec的toUnicode方法来显示中文
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel hello(QObject::tr("你好").toLocal8Bit());
QTextCodec *codec = QTextCodec::codecForLocale();
QString a = codec->toUnicode("安师大手动");
hello.setWindowTitle(a);
hello.show();
return app.exec();
}
小结:对于Qt中文显示问题解决,本篇文章介绍完了,这篇文章应该对你很有用!希望能帮到你吧!
AD:WOT2015 互联网运维与开发者大会 热销抢票
Qt中文显示问题的问题,很多编程人员容易头疼的问题,小细节容易忽略,刚刚编写好的程序,运行之后可能会出现不显示或者乱码这种情况,QT默认的编码(unicode)是不能显示中文的,可能由于windows的默认编码的问题,windows默认使用(GBK/GB2312/GB18030),所以需要来更改QT程序的编码来解决中文显示的问题。
QT中有专门的一个类来处理编码的问题(QTextCodec)。
在QT3中,QApplication可以设置程序的默认编码,但是在QT4中已经没有了该成员函数。可以以下的这些方法来设置编码。
1. 设置QObject的成员函数tr()的编码。
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
Searches all installed QTextCodec objects and returns the one which best matches name; the match is case-insensitive.
Returns 0 if no codec matching the name name could be found.
其中的codecForName函数是根据参数中的编码名称,在系统已经安装的编码方案中需找最佳的匹配编码类型,该查找是大小写不敏感的。如果没有找到,就返回0。
具体的转换代码看下面:
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QLabel hello(QObject::tr("你好"));
hello.setWindowTitle(QObject::tr("终于搞定中文"));
hello.show();
return app.exec();
}
注意:
setCodecForTr一定要在QApplication后面。不然没有效果。而且这种方法只会转换经过tr函数的字符串,并不转换不经过tr函数的字符串。
技巧:
可以用codecForLocale函数来返回现在系统的默认编码,这样更容易做多编码的程序而不用自己手动来更改具体的编码。
2. 使用QString的fromLocal8Bit()函数
这个方法是最快的,系统直接自动将char *的参数转换成为系统默认的编码,然后返回一个QString。
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
// QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
// QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
// QLabel hello(QObject::tr("你好"));
// QLabel hello("你好");
// hello.setWindowTitle(QObject::tr("终于搞定中文"));
QString str;
strstr = str.fromLocal8Bit("哈哈哈");
hello.setWindowTitle(str);
hello.show();
return app.exec();
}
3. 用QTextCodec的toUnicode方法来显示中文
#include <QApplication>
#include <QTextCodec>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel hello(QObject::tr("你好").toLocal8Bit());
QTextCodec *codec = QTextCodec::codecForLocale();
QString a = codec->toUnicode("安师大手动");
hello.setWindowTitle(a);
hello.show();
return app.exec();
}
小结:对于Qt中文显示问题解决,本篇文章介绍完了,这篇文章应该对你很有用!希望能帮到你吧!
相关文章推荐
- qt5.6 中文字体显示乱码问题解决方法
- 解决QT中文字符串在osg中显示乱码的问题
- 某些开发版中的Qt Designer菜单等中文显示为乱码问题解决方法初探
- 解决qt程序在板子上不能正常显示中文的问题
- 解决qt中文显示乱码的问题
- QT之解决按钮显示中文乱码问题
- QT如何显示中文,解决乱码问题
- Qt技巧:中文显示乱码问题解决
- 解决QT发布后中文不显示的问题
- Qt qsqlquerymodel 中文显示乱码问题解决
- 解决linux下qvfb中QT的中文显示问题
- 关于Qt移植后环境变量的设置 qt移植到arm中文显示问题解决 实现pc与tiny210 arm开发板 udp通信(续上)
- QT-解决中文显示乱码问题
- Qt5.7在AM3358下解决中文乱码显示问题
- Qt5.7在AM3358下解决中文乱码显示问题
- Qt中文显示问题解决(转载)
- QT解决中文乱码显示问题
- 解决 Ubuntu 14.04 下 Qt5 中文显示和中文输入法问题
- 【Qt开发】解决Qt5.7.0中文显示乱码的问题
- 解决QT中文字符串在osg中显示乱码的问题