qtencodingutf-8iniqsettings

How can I use QSettings to write UTF-8 characters into [section] and [name] of *.ini file properly?


My code snippet is here:

QSettings setting("xxx.ini", QSettings::Format::IniFormat);
setting.setIniCodec(QTextCodec::codecForName("UTF-8"));
setting.beginGroup(u8"运动控制器");
setting.setValue(u8"运动控制器", u8"运动控制器");
setting.endGroup();

But what is written looks like this:

[%U8FD0%U52A8%U63A7%U5236%U5668]
%U8FD0%U52A8%U63A7%U5236%U5668=运动控制器

So it seems I did set the encoding correctly (partly), but what should I do to change the section and name into text from some per-cent-sign code?

Environment is Qt 5.12.11 and Visual Studio 2019


Solution

  • Unfortunately, this is hard-coded behavior in QSettings that you simply cannot change.

    In section and key names, Unicode characters <= U+00FF (other than a..z, A..Z, 0..9, _, -, or .) are encoded in %XX hex format, and higher characters are encoded in %UXXXX format. The codec specified in setIniCodec() has no effect on this behavior.

    Key values are written in the specified codec, in this case UTF-8.