Qt6 以后,QSettings 读取ini 文件固定使用utf-8 编码
RT,Qt6 以前,QSettngs 对象有个setIniCodec
方法,可以设置编码,但是Qt6 以后就没了,现在ini 文件的读写编码固定为UTF-8。
Qt6 的文档:
In line with most implementations today, QSettings will assume that values in the INI file are UTF-8 encoded. This means that values will be decoded as UTF-8 encoded entries and written back as UTF-8. To retain backward compatibility with older Qt versions, keys in the INI file are written in %-encoded format, but can be read in both %-encoded and UTF-8 formats.
Compatibility with older Qt versions
Please note that this behavior is different to how
QSettings
behaved in versions of Qt prior to Qt 6. INI files written with Qt 5 or earlier are however fully readable by a Qt 6 based application (unless a ini codec different from utf8 had been set). But INI files written with Qt 6 will only be readable by older Qt versions if you set the “iniCodec” to a UTF-8 textcodec.
翻译:
与目前大多数实现一样,QSettings
将假定 INI 文件中的值是 UTF-8 编码的。这意味着值将被解码为 UTF-8 编码条目,并以 UTF-8 写回。为了保持与旧版 Qt 的向后兼容性,INI 文件中的键值以 %-encoded 格式写入,但可以以 %-encoded 和 UTF-8 两种格式读取。
与旧版本 Qt 的兼容性
请注意,这一行为与 Qt 6 之前的 Qt 版本中 QSettings
的行为不同。使用 Qt 5 或更早版本编写的 INI 文件完全可以被基于 Qt 6 的应用程序读取(除非设置了不同于 utf8 的 ini 编解码器)。但只有将 “iniCodec ”设置为UTF-8 文本编解码器,用 Qt 6 编写的 INI 文件才能被旧版本的 Qt 读取。
总之就是省事了,不用管编码问题。不过有一点要注意,这里说是“值” 用utf-8 编码,所以值里可以兼容中文,但是键似乎还不一定。考虑键是QString,既然QString 能支持unicode,那按理说键里也应该能用中文,但也没准儿,懒得试。