将Qt默认编码设置为UTF-8 [英] Set Qt default encoding to UTF-8

查看:999
本文介绍了将Qt默认编码设置为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Qt应用程序中,我的源代码文件编码为UTF-8。对于以下代码...

  QMessageBox :: critical(this,Nepoznatapogreška,Dogodila se nepoznatapogreška! li zatvoriti ovaj program?,QMessageBox :: Yes,QMessageBox :: No); 

...当我显示该消息框时,字符š不会显示为š,但是有点奇怪。这是因为Qt转换所有的C字符串,就好像它们是使用LATIN-1编码的。为了解决这个问题,我一直在使用:

  QMessageBox :: critical(this,QString :: fromUtf8(Nepoznatapogreška) ,QString :: fromUtf8(Dogodila se nepoznatapogreška!Želiteli zatvoriti ovaj program?),QMessageBox :: Yes,QMessageBox :: No); 

有没有办法摆脱所有调用 QString ::您是否尝试过使用 .qt.io / qt-4.8 / qtextcodec.html#setCodecForCStringsrel =nofollow> QTextCodec :: setCodecForCStrings(QTextCodec :: codecForName(UTF-8))


In my Qt application my source code files are encoded as UTF-8. For the following code...

QMessageBox::critical(this, "Nepoznata pogreška", "Dogodila se nepoznata pogreška! Želite li zatvoriti ovaj program ?", QMessageBox::Yes, QMessageBox::No);

...when I show that message box, the character "š" wouldn't be displayed as "š", but as something strange. This is because Qt converts all C-strings as if they are encoded using LATIN-1. To solve this I've been using:

QMessageBox::critical(this, QString::fromUtf8("Nepoznata pogreška"), QString::fromUtf8("Dogodila se nepoznata pogreška! Želite li zatvoriti ovaj program ?"), QMessageBox::Yes, QMessageBox::No);

Is there a way to get rid of all the calls to QString::fromUtf8()?

解决方案

Have you tried using QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"))?

这篇关于将Qt默认编码设置为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆