Qt中文显示
来源:互联网 发布:高级软件测试技术 编辑:程序博客网 时间:2024/05/16 04:51
QT中有专门的一个类来处理编码的问题(QTextCodec)。
在QT3中,QApplication可以设置程序的默认编码,但是在QT4中已经没有了该成员函数。
可以以下的这些方法来设置编码。
1. 设置QObject的成员函数tr()的编码。
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
其中的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("Qt中文显示"));
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);
QString str;
str = str.fromLocal8Bit("Qt中文显示");
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("Qt中文显示");
hello.setWindowTitle(a);
hello.show();
return app.exec();
}
- QT中文显示
- Qt中文显示
- qt中文显示
- QT中文显示
- Qt显示中文
- qt 显示中文
- Qt 中文显示
- QT中文显示问题
- QT中文显示
- QT显示中文
- QT显示中文
- Qt中文显示乱码
- Qt中文显示
- WinCE QT中文显示
- QT显示中文
- Qt中显示中文
- QT显示中文
- Qt 显示中文
- MYSQL 效率优化 20+
- 深入浅出 Cocoa 多线程编程之 block 与 dispatch quene
- 淘宝退货 物流选择
- 指针与数组是如何访问的——可以用extern int *p作为int p[N]的外部声明吗?
- 我最爱祖国
- Qt中文显示
- C语言浮点数比较大小
- Android Dev Tips (持续更新)
- 在Ubuntu10.04中建立ARM交叉编译环境
- Core Data - 框架详解
- Java Inner Class 整理
- 重启,关机代码
- uiimage 图片缩放
- 详细设计说明书