Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSettings - make INI format case-sensitive on windows
QtWS25 Last Chance

QSettings - make INI format case-sensitive on windows

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 676 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T4mmi
    wrote on 1 Aug 2023, 08:05 last edited by
    #1

    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 using QSettings::registerFormat(..., Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) but how can I use it without re-writing all the read/write methods ?

    A 1 Reply Last reply 1 Aug 2023, 09:11
    0
    • T T4mmi
      1 Aug 2023, 08:05

      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 using QSettings::registerFormat(..., Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) but how can I use it without re-writing all the read/write methods ?

      A Offline
      A Offline
      Alain38 0
      wrote on 1 Aug 2023, 09:11 last edited by
      #2

      @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.

      T 1 Reply Last reply 2 Aug 2023, 08:16
      0
      • A Alain38 0
        1 Aug 2023, 09:11

        @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.

        T Offline
        T Offline
        T4mmi
        wrote on 2 Aug 2023, 08:16 last edited by
        #3

        @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 :(

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on 3 Aug 2023, 07:10 last edited by
          #4

          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).

          T 1 Reply Last reply 3 Aug 2023, 07:21
          0
          • S SimonSchroeder
            3 Aug 2023, 07:10

            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).

            T Offline
            T Offline
            T4mmi
            wrote on 3 Aug 2023, 07:21 last edited by
            #5

            @SimonSchroeder I will try this ... just have to find the writeFunc code in the Qt codebase ...

            1 Reply Last reply
            0

            1/5

            1 Aug 2023, 08:05

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved