QSettings does not handle unicode well

I use QSettings to store some parameters in an INI file. However, my program is not in English, so some of the parameters contain Unicode strings. It seems that Qt writes INI files not in utf8 or utf16, but in some other encoding the line "Hello world!" (rus. "Hello world!") looks like this:

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

I want to edit the settings file manually, but I can’t work with it. Is there a way to force Qt to persist in Unicode?

+6
source share
1 answer

Check the setIniCodec QSettings function

Installs a codec for accessing INI files (including Unix .conf files) to a codec. The codec is used to decode any data that is read from an INI file and to encode any data that is written to a file. By default, no codec is used, and non-ASCII characters are encoded using standard INI escape sequences.

So, you should call him with the right codec, for example

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

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

+16
source

Source: https://habr.com/ru/post/901000/


All Articles