Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSettings doesn't handle unicode well

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?

like image 990
Septagram Avatar asked Nov 08 '11 07:11

Septagram


1 Answers

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.

like image 141
pnezis Avatar answered Nov 02 '22 10:11

pnezis