QSettings - make INI format case-sensitive on windows
-
Hi, i'm trying to wrap a config file created (and used otherwise) with python ConfigParser using a QSettings in my app so it will sync the in-app settings changes automatically.
on paper everything works ... except that the app i'm extending REQUIRES the default section to be named "general" .. not "General" :(
It seems that QSettings (on Windows at least) overwrite my defaultsection using a caps-leading case, breaking my app intergration ...
Here is the code (i'm using PyQt5):
root = Path(__file__).parent.parent.resolve() __settings__ = QSettings(str(root / "metadata.txt"), QSettings.IniFormat) __setings__.setValue("foo", "hello") __settings__.setValue("path/root", root)
file before:
[general] name = something
file after:
[General] name = something foo=hello [path] root = ./root
How can I prevent it from messing with the case in sections ?
I saw something usingQSettings::registerFormat(..., Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
but how can I use it without re-writing all the read/write methods ? -
@T4mmi In QSettings documentation it is written "The INI file format has severe restrictions on the syntax of a key. Qt works around this by using % as an escape character in keys. In addition, if you save a top-level setting (a key with no slashes in it, e.g., "someKey"), it will appear in the INI file's "General" section. To avoid overwriting other keys, if you save something using a key such as "General/someKey", the key will be located in the "%General" section, not in the "General" section"
As INI file is case-insensitive, it is why it has changed he name of the section. You might try by using beginGroup("general"), andGroup() around the setValue to check if it uses the default group or create a "%general" group.
-
@Alain38-0 thanks for the time ...
I know th INI format has limitations ... but I have no handle on the choice made by the third party housing app :(I understand that
%General
thing but it's unrelated, the INI format asserts a "general" section by default (if you dont provide one) ... my problem is that it is case-INSENSITIVE on unix ... BUT case-SENSISTIVE on windows (?!) and ther is no way to force (or at least not mess) with the case of already existing general/default section.Using
__settings__.setIniCodec(QTextCodec.codecForName("UTF-8"))
as suggested by chatGPT didn't change a thing (UTF8 is the default codec) sadly.in the example on my first post you can see that for the non-default section, even on windows the case isn't messed up ... only the general one :(
-
I don't think you can force Qt's IniFormat to behave differently. However, you can register your own format to QSettings and implement a QSettings::ReadFunc and QSettings::WriteFunc. Hopefully, you could copy the corresponding source from the IniFormat and just adapt the
General
section to be written lower-case. If you do copy (and adapt) from Qt's source these two functions would be under the GPL/LGPL (unless you have a commercial license, I guess). -
@SimonSchroeder I will try this ... just have to find the writeFunc code in the Qt codebase ...