QSettings doesn't handle unicode well

孤街醉人 提交于 2019-12-01 17:24:09

问题


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!

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?


回答1:


Check the setIniCodec function of QSettings

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");

Notice that you must call it immediately after creating the QSettings objects and before accessing any data.



来源:https://stackoverflow.com/questions/8047321/qsettings-doesnt-handle-unicode-well

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!