Qt显示中文的 方法
2011-09-06 10:28
323 查看
实际发布Qt在显示中文的时候会经常莫名的时好时坏。
在此整理了一些比较可靠的方法与一些备选方法。
1最快的方法:
使用QString的formLocal8bit()
此方法为:系统直接自动将char * 的参数转换成为系统默认的编码,然后返回一个QString。
#include <QTextCodec>
……
{
……
QString str;
str = str.fromLocal8Bit("Qt中文显示");
hello.setWindowTitle(str);
……
}
2我使用的方法:
(发布的时候不需要包含qcncodecs4.dll)
#include <QTextCodec>
int main(int argc, char *argv[])
{
……
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
……
}
另外还有下面几种方法可以试试:
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));
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();
}
在此整理了一些比较可靠的方法与一些备选方法。
1最快的方法:
使用QString的formLocal8bit()
此方法为:系统直接自动将char * 的参数转换成为系统默认的编码,然后返回一个QString。
#include <QTextCodec>
……
{
……
QString str;
str = str.fromLocal8Bit("Qt中文显示");
hello.setWindowTitle(str);
……
}
2我使用的方法:
(发布的时候不需要包含qcncodecs4.dll)
#include <QTextCodec>
int main(int argc, char *argv[])
{
……
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
……
}
另外还有下面几种方法可以试试:
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB18030"));
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 Designer菜单等中文显示为乱码问题解决方法初探
- Qt5.2显示中文方法!!!
- RedHat6.3(linux)编译Qt4.8.6中文不显示的解决方法
- qt/e在arm板上显示中文的方法
- 【Qt】]两种解决Qt5显示中文乱码的方法
- 【Qt开发】Qt5.7中文显示乱码解决方法两种
- 【Qt】]两种解决Qt5显示中文乱码的方法
- QT中文字体的显示方法
- qt5.6 中文字体显示乱码问题解决方法
- QT5中文显示乱码解决方法
- qt显示中文的方法
- Qt For Symbian 程序标题显示中文的方法
- qt在板子上字很小与中文显示的解决方法
- QT 打印中文显示乱码的解决方法
- 开发板和qt中文显示异常解决方法记录
- qt编写的android程序在android系统中运行,中文显示乱码的解决方法
- qt在板子上字很小与中文显示的解决方法 .
- Qt中显示中文的方法