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. How to effectively parse the contents of an INI file?
Qt 6.11 is out! See what's new in the release blog

How to effectively parse the contents of an INI file?

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

    I have an INI file that looks something like this:

    [General]
    Version=160
    Necessary=true
    

    I do not have access to the INI file itself, but I do have access to the data inside. So I can easily get a string like:

    [General]\nVersion=160\nNecessary=true
    

    And I can easily manipulate that string to get the information I need.

    However, does Qt offer a better way to handle this?

    I thought about using QSettings, but I don't really have access to the file itself, as I said.

    JonBJ 1 Reply Last reply
    0
    • H heftlas

      @JonB
      I can't really pass the string as a parameter like this, though, right?

      // settings is just a member variable of type QSettings*
       settings = new QSettings("[General]\nVersion=160\nNecessary=true", QSettings::IniFormat);
      

      I tried that, but it just returns an empty QSettings object.

      I just want an easier way to handle a string that is formatted like an INI file. Preferably without writing the file out, as that INI file isn't really supposed to be available to everyone, but I don't mind a solution that writes it out for the moment.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #8

      @heftlas said in How to effectively parse the contents of an INI file?:

      I can't really pass the string as a parameter like this, though, right?

      No, it does not have a constructor which takes a string and parses it. But if you look at beginGroup() and setValue() you can construct an in-memory QSettings which matches your string.

      If you want something which parses an existing string, without having that in a file which can be read, QSettings won't do that. Unless you want to write that string to a file, get QSettings to parse that, and then delete the file.

      H 1 Reply Last reply
      1
      • O Offline
        O Offline
        ollarch
        wrote on last edited by
        #2

        If you have the data as a string, write it to a file and read it using QSettings.

        H 1 Reply Last reply
        0
        • O ollarch

          If you have the data as a string, write it to a file and read it using QSettings.

          H Offline
          H Offline
          heftlas
          wrote on last edited by
          #3

          @ollarch said in How to effectively parse the contents of an INI file?:

          If you have the data as a string, write it to a file and read it using QSettings.

          Do you know of any other way? Writing it to a file just to read it and probably then delete that file just seems wrong to me. If it comes to that, then probably creating my own INI parser is better…but I believe Qt would have something like this done for me already.

          JonBJ 1 Reply Last reply
          0
          • H heftlas

            @ollarch said in How to effectively parse the contents of an INI file?:

            If you have the data as a string, write it to a file and read it using QSettings.

            Do you know of any other way? Writing it to a file just to read it and probably then delete that file just seems wrong to me. If it comes to that, then probably creating my own INI parser is better…but I believe Qt would have something like this done for me already.

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

            @heftlas
            It does. QSettings will read the INI file you show. And also write it if you need that too.

            I thought about using QSettings, but I don't really have access to the file itself, as I said.

            I don't know what this means, or what your question about this is.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              heftlas
              wrote on last edited by heftlas
              #5

              @JonB
              I don't have access to the file “example.ini” (it isn't in my computer) I only have access to its contents—the string I showed. One solution, as pointed by @ollarch is to write it to a file and then read it using QSettings, but I feel like that's overkill.

              1 Reply Last reply
              0
              • H heftlas

                I have an INI file that looks something like this:

                [General]
                Version=160
                Necessary=true
                

                I do not have access to the INI file itself, but I do have access to the data inside. So I can easily get a string like:

                [General]\nVersion=160\nNecessary=true
                

                And I can easily manipulate that string to get the information I need.

                However, does Qt offer a better way to handle this?

                I thought about using QSettings, but I don't really have access to the file itself, as I said.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @heftlas said in How to effectively parse the contents of an INI file?:

                However, does Qt offer a better way to handle this?
                I thought about using QSettings, but I don't really have access to the file itself, as I said.

                Yes, Qt offers a better way to handle this. It is via QSettings.

                You do not need to have the existing file. You can create what you have in QSettings from code without reading a file. It will write the file out, but what do you care?

                So I can easily get a string like:
                [General]\nVersion=160\nNecessary=true

                And I can easily manipulate that string to get the information I need.

                If that is what you want to do without going via QSettings then there is no point asking about using QSettings instead.

                If this does not answer your question I really do not understand exactly what you are looking for.

                H 1 Reply Last reply
                0
                • JonBJ JonB

                  @heftlas said in How to effectively parse the contents of an INI file?:

                  However, does Qt offer a better way to handle this?
                  I thought about using QSettings, but I don't really have access to the file itself, as I said.

                  Yes, Qt offers a better way to handle this. It is via QSettings.

                  You do not need to have the existing file. You can create what you have in QSettings from code without reading a file. It will write the file out, but what do you care?

                  So I can easily get a string like:
                  [General]\nVersion=160\nNecessary=true

                  And I can easily manipulate that string to get the information I need.

                  If that is what you want to do without going via QSettings then there is no point asking about using QSettings instead.

                  If this does not answer your question I really do not understand exactly what you are looking for.

                  H Offline
                  H Offline
                  heftlas
                  wrote on last edited by
                  #7

                  @JonB
                  I can't really pass the string as a parameter like this, though, right?

                  // settings is just a member variable of type QSettings*
                   settings = new QSettings("[General]\nVersion=160\nNecessary=true", QSettings::IniFormat);
                  

                  I tried that, but it just returns an empty QSettings object.

                  I just want an easier way to handle a string that is formatted like an INI file. Preferably without writing the file out, as that INI file isn't really supposed to be available to everyone, but I don't mind a solution that writes it out for the moment.

                  JonBJ 1 Reply Last reply
                  0
                  • H heftlas

                    @JonB
                    I can't really pass the string as a parameter like this, though, right?

                    // settings is just a member variable of type QSettings*
                     settings = new QSettings("[General]\nVersion=160\nNecessary=true", QSettings::IniFormat);
                    

                    I tried that, but it just returns an empty QSettings object.

                    I just want an easier way to handle a string that is formatted like an INI file. Preferably without writing the file out, as that INI file isn't really supposed to be available to everyone, but I don't mind a solution that writes it out for the moment.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @heftlas said in How to effectively parse the contents of an INI file?:

                    I can't really pass the string as a parameter like this, though, right?

                    No, it does not have a constructor which takes a string and parses it. But if you look at beginGroup() and setValue() you can construct an in-memory QSettings which matches your string.

                    If you want something which parses an existing string, without having that in a file which can be read, QSettings won't do that. Unless you want to write that string to a file, get QSettings to parse that, and then delete the file.

                    H 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @heftlas said in How to effectively parse the contents of an INI file?:

                      I can't really pass the string as a parameter like this, though, right?

                      No, it does not have a constructor which takes a string and parses it. But if you look at beginGroup() and setValue() you can construct an in-memory QSettings which matches your string.

                      If you want something which parses an existing string, without having that in a file which can be read, QSettings won't do that. Unless you want to write that string to a file, get QSettings to parse that, and then delete the file.

                      H Offline
                      H Offline
                      heftlas
                      wrote on last edited by
                      #9

                      @JonB Exactly what I was looking for! Thank you for your help! And sorry if I wasn't very clear at first.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        Hi,

                        For that use case, you might be interested by QTemporaryFile.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        • Login

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