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 adds % sign with comments

QSettings adds % sign with comments

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.4k 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.
  • B Offline
    B Offline
    Bremenpl
    wrote on last edited by
    #1

    Hello there,
    I am using Qt 5.13 and the QSettings class. Here is my exemplary INI file:

    [IMO9490052]
    ServerUrl=opc.tcp://192.168.56.101:4841
    
    #[IMO9490040]
    #ServerUrl=opc.tcp://192.168.56.101:4841
    

    The first group and section are valid, the second group and section are commented out. I then do

    	QSettings cfgv("the path", QSettings::IniFormat);
    	cfgv.beginGroup("IMO9490052");
    	cfgv.setValue("ServerUrl", "someString);
    	cfgv.endGroup();
    

    The Ini file is then modified like this:

    [IMO9490052]
    ServerUrl=someString
    %23ServerUrl=opc.tcp://192.168.56.101:4841
    

    The commented out section got corrupted somehow and % sign was added.
    What could be the case here? Would appreciate all help.

    lprzenioslo.zut.edu.pl

    JonBJ 1 Reply Last reply
    0
    • B Bremenpl

      Hello there,
      I am using Qt 5.13 and the QSettings class. Here is my exemplary INI file:

      [IMO9490052]
      ServerUrl=opc.tcp://192.168.56.101:4841
      
      #[IMO9490040]
      #ServerUrl=opc.tcp://192.168.56.101:4841
      

      The first group and section are valid, the second group and section are commented out. I then do

      	QSettings cfgv("the path", QSettings::IniFormat);
      	cfgv.beginGroup("IMO9490052");
      	cfgv.setValue("ServerUrl", "someString);
      	cfgv.endGroup();
      

      The Ini file is then modified like this:

      [IMO9490052]
      ServerUrl=someString
      %23ServerUrl=opc.tcp://192.168.56.101:4841
      

      The commented out section got corrupted somehow and % sign was added.
      What could be the case here? Would appreciate all help.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @bremenpl
      Qt's QSettings is not the same thing as a Windows .ini file. It has a syntax which is "similar", but not the same. Unlike INIs, it does not have any "comment marker". So the hash (#) characters you have are treated as a literal part of the body. Because # is a "special" character, QSettings encodes it as hex %23, that's why you see that, but otherwise it's functionally identical to a literal #, and that is what you'll see you get back when QSettings parses that file.

      B 1 Reply Last reply
      5
      • JonBJ JonB

        @bremenpl
        Qt's QSettings is not the same thing as a Windows .ini file. It has a syntax which is "similar", but not the same. Unlike INIs, it does not have any "comment marker". So the hash (#) characters you have are treated as a literal part of the body. Because # is a "special" character, QSettings encodes it as hex %23, that's why you see that, but otherwise it's functionally identical to a literal #, and that is what you'll see you get back when QSettings parses that file.

        B Offline
        B Offline
        Bremenpl
        wrote on last edited by
        #3

        @jonb Thank you for answer.
        Is there a way to instruct the QSettings object that I want to set some sign as a comment indicator?

        lprzenioslo.zut.edu.pl

        JonBJ Pablo J. RoginaP 2 Replies Last reply
        0
        • B Bremenpl

          @jonb Thank you for answer.
          Is there a way to instruct the QSettings object that I want to set some sign as a comment indicator?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @bremenpl
          No, that's why I said: it does not have any "comment marker"!

          1 Reply Last reply
          0
          • B Bremenpl

            @jonb Thank you for answer.
            Is there a way to instruct the QSettings object that I want to set some sign as a comment indicator?

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @bremenpl I guess you hit a bug in QSettings. Given the example you provided, both the data and the code the commented line should not be modified.

            What happens if you use hierarchical keys instead of the begin()/end() approach i.e.

            cfgv.setValue("IMO9490052/ServerUrl", "someString);
            

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            B 1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi
              Its by design.
              https://doc.qt.io/qt-5/qsettings.html#Format-enum
              section

              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.

              1 Reply Last reply
              2
              • Pablo J. RoginaP Pablo J. Rogina

                @bremenpl I guess you hit a bug in QSettings. Given the example you provided, both the data and the code the commented line should not be modified.

                What happens if you use hierarchical keys instead of the begin()/end() approach i.e.

                cfgv.setValue("IMO9490052/ServerUrl", "someString);
                
                B Offline
                B Offline
                Bremenpl
                wrote on last edited by
                #7

                @pablo-j-rogina I confirm that it doesnt work this way as well- no change.

                lprzenioslo.zut.edu.pl

                1 Reply Last reply
                0

                • Login

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