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. Reading [General] section in ini-files
Forum Updated to NodeBB v4.3 + New Features

Reading [General] section in ini-files

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 2.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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Copy the ini-file, replace [General] with something different and read this.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    E 1 Reply Last reply
    1
    • Christian EhrlicherC Christian Ehrlicher

      Copy the ini-file, replace [General] with something different and read this.

      E Offline
      E Offline
      ErikThomsen
      wrote on last edited by
      #3

      @Christian-Ehrlicher
      Unfortunately, this is not possible, as this is a large collection of ini-files that a number of different software tools are already using. Modifying the ini-files is not an option.

      Christian EhrlicherC 1 Reply Last reply
      0
      • E ErikThomsen

        @Christian-Ehrlicher
        Unfortunately, this is not possible, as this is a large collection of ini-files that a number of different software tools are already using. Modifying the ini-files is not an option.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @ErikThomsen Why can't you copy the ini file before trying to read it with Qt?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        E 1 Reply Last reply
        2
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #5

          or, if copying is not possible, why not read it in memory change General there and read it than with QSettings

          surely the file is not Gbytes big ?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          E 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @ErikThomsen Why can't you copy the ini file before trying to read it with Qt?

            E Offline
            E Offline
            ErikThomsen
            wrote on last edited by
            #6

            @Christian-Ehrlicher
            It is not one file. It is a large number of files, full of configuration data, used by numerous different software tool.
            These files gradualy increase in number, and occationally the contents change. Therefore, we need a common pool of these files, so only one set of files is maintained.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #7

              I still don't understand why you can't copy the file to a temp dir, modify it and read it with Qt...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                or, if copying is not possible, why not read it in memory change General there and read it than with QSettings

                surely the file is not Gbytes big ?

                E Offline
                E Offline
                ErikThomsen
                wrote on last edited by
                #8

                @J-Hilk
                No, the files are not huge. And your solution would probably work.
                My own solution will probaby be something similar, such as parsing the [General] section as a text file. All the other sections work as they should.

                I just hoped that someone had found a way to tweak QSettings directly.

                @Christian-Ehrlicher
                In that case, it will be necessary for the software to do this everytime it is used. I find that impratical.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #9

                  @ErikThomsen said in Reading [General] section in ini-files:

                  In that case, it will be necessary for the software to do this everytime it is used. I find that impratical.

                  And what's different from your / @J-Hilk's idea? It's a simple class derived from QSettings...

                  ok, deriving does not work due to QSettings constraints, ~12 lines of code without error checking...

                  class MySettings
                  {
                  public:
                    MySettings(const QString &fn)
                    {
                      QFile f(fn);
                      f.open(QIODevice::ReadOnly);
                      QByteArray ba = f.readAll();
                      f.close();
                      temp.open();
                      temp.write(ba.replace("[General]", "[Blub]"));
                      temp.close();
                      m_settings = new QSettings(temp.fileName(), QSettings::IniFormat);
                    }
                    ~MySettings()
                    {
                      delete m_settings;
                    }
                    QSettings *settings()
                    {
                      return m_settings;
                    }
                  private:
                    QSettings *m_settings = nullptr;
                    QTemporaryFile temp;
                  }
                  

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  E 1 Reply Last reply
                  3
                  • S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #10

                    We let our QSettings write to the default location. So, I don't really know how Windows handles this (the default location on Windows is the registry). But I had a look what Linux does. What I found there is that if I don't specify any group values are put under the section [General]. Maybe the only thing you have to do to read the [General] section is to not use a group. It might be that just for your own group General Qt will put a % in front of the name to distinguish it from the non-group stuff.

                    E 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @ErikThomsen said in Reading [General] section in ini-files:

                      In that case, it will be necessary for the software to do this everytime it is used. I find that impratical.

                      And what's different from your / @J-Hilk's idea? It's a simple class derived from QSettings...

                      ok, deriving does not work due to QSettings constraints, ~12 lines of code without error checking...

                      class MySettings
                      {
                      public:
                        MySettings(const QString &fn)
                        {
                          QFile f(fn);
                          f.open(QIODevice::ReadOnly);
                          QByteArray ba = f.readAll();
                          f.close();
                          temp.open();
                          temp.write(ba.replace("[General]", "[Blub]"));
                          temp.close();
                          m_settings = new QSettings(temp.fileName(), QSettings::IniFormat);
                        }
                        ~MySettings()
                        {
                          delete m_settings;
                        }
                        QSettings *settings()
                        {
                          return m_settings;
                        }
                      private:
                        QSettings *m_settings = nullptr;
                        QTemporaryFile temp;
                      }
                      
                      E Offline
                      E Offline
                      ErikThomsen
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher
                      I just need to read some pre-made ini-files. Not modify them.
                      But I have solved the issue by reading the ini-file as a text file, and parsing the lines in the [General] section manually.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @ErikThomsen said in Reading [General] section in ini-files:

                        I just need to read some pre-made ini-files. Not modify them.

                        I never said that you should modify them... read my osts and suggestion properly!

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        E 1 Reply Last reply
                        0
                        • S SimonSchroeder

                          We let our QSettings write to the default location. So, I don't really know how Windows handles this (the default location on Windows is the registry). But I had a look what Linux does. What I found there is that if I don't specify any group values are put under the section [General]. Maybe the only thing you have to do to read the [General] section is to not use a group. It might be that just for your own group General Qt will put a % in front of the name to distinguish it from the non-group stuff.

                          E Offline
                          E Offline
                          ErikThomsen
                          wrote on last edited by
                          #13

                          @SimonSchroeder
                          Aaaah! That's interesting!
                          Will try it. Thanks!

                          Due to the posting frequency limit for newbies, I had plenty om time to try it out.
                          It works nicely.

                          Instead of

                          settings.beginGroup("General");
                          

                          which accesses [%General], I use

                          settings.beginGroup("");
                          

                          which accesses [General]. The compiler gives me a warning, but that can be ignored.

                          S 1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            @ErikThomsen said in Reading [General] section in ini-files:

                            I just need to read some pre-made ini-files. Not modify them.

                            I never said that you should modify them... read my osts and suggestion properly!

                            E Offline
                            E Offline
                            ErikThomsen
                            wrote on last edited by
                            #14

                            @Christian-Ehrlicher
                            I apologize for being stupid. If you didn't mean to copy and modify the files, then I simply don't understand your suggestion.

                            In any case, the hint from @SimonSchroeder solved the issue.

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              I use the file, generate a modified temoprary file and use this for QSettings. The temporary file is deleted right after the class gets destructed. So it's the same as @J-Hilk suggested. I never modified the original one so I don't see what's your problem with my suggestion...

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              1
                              • E ErikThomsen

                                @SimonSchroeder
                                Aaaah! That's interesting!
                                Will try it. Thanks!

                                Due to the posting frequency limit for newbies, I had plenty om time to try it out.
                                It works nicely.

                                Instead of

                                settings.beginGroup("General");
                                

                                which accesses [%General], I use

                                settings.beginGroup("");
                                

                                which accesses [General]. The compiler gives me a warning, but that can be ignored.

                                S Offline
                                S Offline
                                SimonSchroeder
                                wrote on last edited by
                                #16

                                @ErikThomsen said in Reading [General] section in ini-files:

                                I use
                                settings.beginGroup("");

                                which accesses [General]. The compiler gives me a warning, but that can be ignored.

                                I am not sure what the compiler warns about. But, you don't have to start any group. Just drop the beginGroup(...) altogether (and also the corresponding endGroup()).

                                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