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 not returning keys of the predefined .ini file
Forum Update on Monday, May 27th 2025

QSettings not returning keys of the predefined .ini file

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 2.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.
  • T Offline
    T Offline
    thippu
    wrote on 1 Oct 2018, 07:17 last edited by thippu 10 Jan 2018, 07:18
    #1

    Hi,
    I'm having predefined.ini file
    It's contents are:

    [Personal]//I want these to be groups in the QSettings object, Later I may display the contents of each group.
    Name=Abc
    Age=10
    [Education]
    Highest=Phd
    [Interests]
    hobby=volleyball
    [these_may_grow]// I need to scan all the [ ]
    these_may_grow=these_may_grow
    

    I have global QSettings object, I initialized in the constructor, now I want to read the predefined .ini file and create a group([contents of this]).

    code to retrieve values in predefined .ini I have developed is :

    iniSettings->setPath(QSettings::IniFormat,QSettings::UserScope,filename);// filename is a global variable here
    //If I do the following codes 
    qDebug()<<iniSettings->group();//returns " "
    qDebug()<<iniSettings->allKeys();//returns ()
    

    Where am I failing?

    A R 2 Replies Last reply 1 Oct 2018, 07:19
    0
    • T thippu
      1 Oct 2018, 07:17

      Hi,
      I'm having predefined.ini file
      It's contents are:

      [Personal]//I want these to be groups in the QSettings object, Later I may display the contents of each group.
      Name=Abc
      Age=10
      [Education]
      Highest=Phd
      [Interests]
      hobby=volleyball
      [these_may_grow]// I need to scan all the [ ]
      these_may_grow=these_may_grow
      

      I have global QSettings object, I initialized in the constructor, now I want to read the predefined .ini file and create a group([contents of this]).

      code to retrieve values in predefined .ini I have developed is :

      iniSettings->setPath(QSettings::IniFormat,QSettings::UserScope,filename);// filename is a global variable here
      //If I do the following codes 
      qDebug()<<iniSettings->group();//returns " "
      qDebug()<<iniSettings->allKeys();//returns ()
      

      Where am I failing?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 1 Oct 2018, 07:19 last edited by aha_1980 10 Jan 2018, 07:22
      #2

      @thippu

      Please check that the filename can be sucessfully resolved.

      Please note that the constructor does not take a filename, but instead an organisation and an application name.

      Regards

      Qt has to stay free or it will die.

      T 1 Reply Last reply 1 Oct 2018, 07:21
      1
      • A aha_1980
        1 Oct 2018, 07:19

        @thippu

        Please check that the filename can be sucessfully resolved.

        Please note that the constructor does not take a filename, but instead an organisation and an application name.

        Regards

        T Offline
        T Offline
        thippu
        wrote on 1 Oct 2018, 07:21 last edited by
        #3

        @aha_1980 Yes, I have checked the filename and it's existence, all are true.

        A 1 Reply Last reply 1 Oct 2018, 07:23
        0
        • T thippu
          1 Oct 2018, 07:21

          @aha_1980 Yes, I have checked the filename and it's existence, all are true.

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 1 Oct 2018, 07:23 last edited by
          #4

          @thippu Please see that I edited my post.

          Qt has to stay free or it will die.

          T 1 Reply Last reply 1 Oct 2018, 07:29
          0
          • T thippu
            1 Oct 2018, 07:17

            Hi,
            I'm having predefined.ini file
            It's contents are:

            [Personal]//I want these to be groups in the QSettings object, Later I may display the contents of each group.
            Name=Abc
            Age=10
            [Education]
            Highest=Phd
            [Interests]
            hobby=volleyball
            [these_may_grow]// I need to scan all the [ ]
            these_may_grow=these_may_grow
            

            I have global QSettings object, I initialized in the constructor, now I want to read the predefined .ini file and create a group([contents of this]).

            code to retrieve values in predefined .ini I have developed is :

            iniSettings->setPath(QSettings::IniFormat,QSettings::UserScope,filename);// filename is a global variable here
            //If I do the following codes 
            qDebug()<<iniSettings->group();//returns " "
            qDebug()<<iniSettings->allKeys();//returns ()
            

            Where am I failing?

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 1 Oct 2018, 07:28 last edited by raven-worx 10 Jan 2018, 07:30
            #5

            @thippu said in QSettings not returning keys of the predefined .ini file:

            iniSettings->setPath(QSettings::IniFormat,QSettings::UserScope,filename);// filename is a global variable here
            //If I do the following codes
            qDebug()<<iniSettings->group();//returns " "
            qDebug()<<iniSettings->allKeys();//returns ()

            you need to call QSettings::beginGroup() before reading/writing
            also you can check if the return value of QFile::exists(filename) if the application really can find your ini-file.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            T 2 Replies Last reply 1 Oct 2018, 07:31
            1
            • A aha_1980
              1 Oct 2018, 07:23

              @thippu Please see that I edited my post.

              T Offline
              T Offline
              thippu
              wrote on 1 Oct 2018, 07:29 last edited by
              #6

              @aha_1980 Yes, I saw it, and I'm sure there is no filename initialization in the constructor, it happens through the QFileDialog.getOpenFilename();
              and for double sure I do if(!QFile(filename).exists()).

              1 Reply Last reply
              0
              • R raven-worx
                1 Oct 2018, 07:28

                @thippu said in QSettings not returning keys of the predefined .ini file:

                iniSettings->setPath(QSettings::IniFormat,QSettings::UserScope,filename);// filename is a global variable here
                //If I do the following codes
                qDebug()<<iniSettings->group();//returns " "
                qDebug()<<iniSettings->allKeys();//returns ()

                you need to call QSettings::beginGroup() before reading/writing
                also you can check if the return value of QFile::exists(filename) if the application really can find your ini-file.

                T Offline
                T Offline
                thippu
                wrote on 1 Oct 2018, 07:31 last edited by
                #7

                @raven-worx Yes, I did qDebug()<<QFile(filename).exists(); it returned me true.

                1 Reply Last reply
                0
                • R raven-worx
                  1 Oct 2018, 07:28

                  @thippu said in QSettings not returning keys of the predefined .ini file:

                  iniSettings->setPath(QSettings::IniFormat,QSettings::UserScope,filename);// filename is a global variable here
                  //If I do the following codes
                  qDebug()<<iniSettings->group();//returns " "
                  qDebug()<<iniSettings->allKeys();//returns ()

                  you need to call QSettings::beginGroup() before reading/writing
                  also you can check if the return value of QFile::exists(filename) if the application really can find your ini-file.

                  T Offline
                  T Offline
                  thippu
                  wrote on 1 Oct 2018, 07:35 last edited by
                  #8

                  @raven-worx for beginGroup() , iniSetting->beginGroup("[");// I doubt it will work as per .ini filestructure?

                  R 2 Replies Last reply 1 Oct 2018, 07:37
                  0
                  • T thippu
                    1 Oct 2018, 07:35

                    @raven-worx for beginGroup() , iniSetting->beginGroup("[");// I doubt it will work as per .ini filestructure?

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 1 Oct 2018, 07:37 last edited by raven-worx 10 Jan 2018, 07:38
                    #9

                    @thippu
                    with respect to your ini file from your first post you should call:

                    iniSettings->beginGroup("Personal");
                    iniSettings->value("Age");
                    iniSettings->endGroup();
                    

                    or alternatively (if you don't have a group set!):

                    iniSettings->value("Personal/Age");
                    

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    T 1 Reply Last reply 1 Oct 2018, 07:41
                    1
                    • A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 1 Oct 2018, 07:40 last edited by
                      #10

                      @thippu

                      And in addition to what @raven-worx says, you need to use this constructor if you want to specify the filename.

                      Otherwise QSettings calculates the filename itself.

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      3
                      • R raven-worx
                        1 Oct 2018, 07:37

                        @thippu
                        with respect to your ini file from your first post you should call:

                        iniSettings->beginGroup("Personal");
                        iniSettings->value("Age");
                        iniSettings->endGroup();
                        

                        or alternatively (if you don't have a group set!):

                        iniSettings->value("Personal/Age");
                        
                        T Offline
                        T Offline
                        thippu
                        wrote on 1 Oct 2018, 07:41 last edited by
                        #11

                        @raven-worx Yes bro, in that .ini example, [groupname] // may grow and I want to scan them dynamically, how to do it?

                        1 Reply Last reply
                        0
                        • T thippu
                          1 Oct 2018, 07:35

                          @raven-worx for beginGroup() , iniSetting->beginGroup("[");// I doubt it will work as per .ini filestructure?

                          R Offline
                          R Offline
                          raven-worx
                          Moderators
                          wrote on 1 Oct 2018, 07:42 last edited by
                          #12

                          @thippu
                          yes i just wanted to add as @aha_1980 said.
                          Use the mentioned constructor, since in your case it is not required to mess with the scopes.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          T 1 Reply Last reply 1 Oct 2018, 07:50
                          1
                          • R raven-worx
                            1 Oct 2018, 07:42

                            @thippu
                            yes i just wanted to add as @aha_1980 said.
                            Use the mentioned constructor, since in your case it is not required to mess with the scopes.

                            T Offline
                            T Offline
                            thippu
                            wrote on 1 Oct 2018, 07:50 last edited by
                            #13

                            @raven-worx okay, I have included in the constructor itself.
                            code: iniSettings=new QSettings(filename,QSettings::IniFormat,this);

                            R 1 Reply Last reply 1 Oct 2018, 08:01
                            0
                            • T thippu
                              1 Oct 2018, 07:50

                              @raven-worx okay, I have included in the constructor itself.
                              code: iniSettings=new QSettings(filename,QSettings::IniFormat,this);

                              R Offline
                              R Offline
                              raven-worx
                              Moderators
                              wrote on 1 Oct 2018, 08:01 last edited by
                              #14

                              @thippu
                              and? it's working now?

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              T 2 Replies Last reply 1 Oct 2018, 08:06
                              1
                              • R raven-worx
                                1 Oct 2018, 08:01

                                @thippu
                                and? it's working now?

                                T Offline
                                T Offline
                                thippu
                                wrote on 1 Oct 2018, 08:06 last edited by
                                #15

                                @raven-worx bro, in .ini

                                ) can grow, I want to scan them dynamically, so what to include in
                                beginGroup() ?, please help me.

                                R 1 Reply Last reply 1 Oct 2018, 08:20
                                0
                                • R raven-worx
                                  1 Oct 2018, 08:01

                                  @thippu
                                  and? it's working now?

                                  T Offline
                                  T Offline
                                  thippu
                                  wrote on 1 Oct 2018, 08:18 last edited by thippu 10 Jan 2018, 08:22
                                  #16

                                  @raven-worx Yes, bro working. failure was the happing because of the constructor, after setting it properly it working now, I did
                                  I can't thank enough to guys @raven-worx , @aha_1980

                                  qDebug()<<iniSettings->childGroups();//returns all the heading names````
                                  1 Reply Last reply
                                  0
                                  • T thippu
                                    1 Oct 2018, 08:06

                                    @raven-worx bro, in .ini

                                    ) can grow, I want to scan them dynamically, so what to include in
                                    beginGroup() ?, please help me.

                                    R Offline
                                    R Offline
                                    raven-worx
                                    Moderators
                                    wrote on 1 Oct 2018, 08:20 last edited by
                                    #17

                                    @thippu
                                    with the usage of the right Constructor your ini-file should be read correctly now.
                                    So now you can use QSettings::childGroups() to traverse all groups and read the values as i described above or with the use of QSettings::beginGroup() and QSettings::childKeys()

                                    foreach( QString group, iniSettings->childGroups() )
                                    {
                                         // process group
                                         iniSettings->beginGroup(group);
                                             foreach( QString key, iniSettings->childKeys() )
                                             {
                                                    QVariant val = iniSettings->value(key);
                                                    // process val
                                             }
                                         iniSettings->endGroup();
                                    }
                                    

                                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                    If you have a question please use the forum so others can benefit from the solution in the future

                                    J.HilkJ T 2 Replies Last reply 1 Oct 2018, 08:23
                                    4
                                    • R raven-worx
                                      1 Oct 2018, 08:20

                                      @thippu
                                      with the usage of the right Constructor your ini-file should be read correctly now.
                                      So now you can use QSettings::childGroups() to traverse all groups and read the values as i described above or with the use of QSettings::beginGroup() and QSettings::childKeys()

                                      foreach( QString group, iniSettings->childGroups() )
                                      {
                                           // process group
                                           iniSettings->beginGroup(group);
                                               foreach( QString key, iniSettings->childKeys() )
                                               {
                                                      QVariant val = iniSettings->value(key);
                                                      // process val
                                               }
                                           iniSettings->endGroup();
                                      }
                                      
                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on 1 Oct 2018, 08:23 last edited by
                                      #18

                                      @raven-worx
                                      uh hu, using deprecated syntax/macros, shame on you ;-)


                                      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.

                                      R 1 Reply Last reply 1 Oct 2018, 09:36
                                      1
                                      • R raven-worx
                                        1 Oct 2018, 08:20

                                        @thippu
                                        with the usage of the right Constructor your ini-file should be read correctly now.
                                        So now you can use QSettings::childGroups() to traverse all groups and read the values as i described above or with the use of QSettings::beginGroup() and QSettings::childKeys()

                                        foreach( QString group, iniSettings->childGroups() )
                                        {
                                             // process group
                                             iniSettings->beginGroup(group);
                                                 foreach( QString key, iniSettings->childKeys() )
                                                 {
                                                        QVariant val = iniSettings->value(key);
                                                        // process val
                                                 }
                                             iniSettings->endGroup();
                                        }
                                        
                                        T Offline
                                        T Offline
                                        thippu
                                        wrote on 1 Oct 2018, 08:25 last edited by
                                        #19

                                        @raven-worx Sure bro, I will use this code to create group and contents of the group.

                                        1 Reply Last reply
                                        0
                                        • J.HilkJ J.Hilk
                                          1 Oct 2018, 08:23

                                          @raven-worx
                                          uh hu, using deprecated syntax/macros, shame on you ;-)

                                          R Offline
                                          R Offline
                                          raven-worx
                                          Moderators
                                          wrote on 1 Oct 2018, 09:36 last edited by
                                          #20

                                          @J.Hilk said in QSettings not returning keys of the predefined .ini file:

                                          uh hu, using deprecated syntax/macros, shame on you ;-)

                                          yes, i am still too lazy for such a convenient function to deprecate it also in my head ;)

                                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                          If you have a question please use the forum so others can benefit from the solution in the future

                                          1 Reply Last reply
                                          0

                                          9/20

                                          1 Oct 2018, 07:37

                                          • Login

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