QSettings不能很好地处理unicode [英] QSettings doesn't handle unicode well

查看:276
本文介绍了QSettings不能很好地处理unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QSettings将某些设置存储在INI文件中.但是,我的程序不是英语,因此某些设置包含Unicode字符串.看来Qt不会以utf8或utf16的形式写入INI文件,而是以其他一些编码形式写入字符串Приветмир!". (rus."Hello world!")看起来像这样:

I'm using QSettings to store some settings in an INI file. However, my program is not in English, so some of the settings contain Unicode strings. It seems that Qt writes INI files not in utf8 or utf16, but in some other encoding, the string "Привет мир!" (rus. "Hello world!") looks like this:

WindowTitle=\x41f\x440\x438\x432\x435\x442 \x43c\x438\x440!

我想手动编辑设置文件,但是我不能完全像这样使用它.有没有一种方法可以强制Qt以Unicode保存?

I want to edit settings file by hand, but I can't quite work with it like this. Is there a way to force Qt to save in Unicode?

推荐答案

检查 setIniCodec QSettings

设置用于访问INI文件(在Unix上包括.conf文件)的编解码器 编解码器.编解码器用于解码从中读取的任何数据 INI文件,并用于编码写入该文件的所有数据. 默认情况下,不使用编解码器,并且对非ASCII字符进行编码 使用标准的INI转义序列.

Sets the codec for accessing INI files (including .conf files on Unix) to codec. The codec is used for decoding any data that is read from the INI file, and for encoding any data that is written to the file. By default, no codec is used, and non-ASCII characters are encoded using standard INI escape sequences.

因此,您应该使用所需的编解码器来调用它,例如

So you should call it with the codec you want, eg

QSettings settings;
settings.setIniCodec("UTF-8");

请注意,您必须在创建QSettings对象之后且访问任何数据之前立即调用它.

这篇关于QSettings不能很好地处理unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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