Qt显示中文的 方法

实际发布Qt在显示中文的时候会经常莫名的时好时坏。

在此整理了一些比较可靠的方法与一些备选方法。

1最快的方法:

使用QStringformLocal8bit()

此方法为:系统直接自动将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.QTextCodectoUnicode方法来显示中文

#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();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值