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. Regarding Using QSettings to read INI File
Forum Updated to NodeBB v4.3 + New Features

Regarding Using QSettings to read INI File

Scheduled Pinned Locked Moved General and Desktop
48 Posts 12 Posters 63.8k Views 4 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.
  • L Offline
    L Offline
    loladiro
    wrote on last edited by
    #8

    Believe me the foreach loop is fast! And it makes it easier shoud you ever change your container e.g. from a QList to a QSet.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Indrajeet
      wrote on last edited by
      #9

      Hi loladiro

      How to retrieve each value from QStringList which we got from foreach loop
      into QString?

      Regards
      Indrajeet

      1 Reply Last reply
      0
      • L Offline
        L Offline
        loladiro
        wrote on last edited by
        #10

        What exactly do you want to do with the values ( I think I can help you better that way)?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #11

          Perhaps, instead of storing your values in two QStringLists, you would like to store them in a single QHash<QString, QString> ?

          That way, at least you keep your keys and values together in the same container...

          To get them, you could modify the example code from above to something like this:
          @
          QSettings settings("test.ini", QSettings::IniFormat);
          settings.beginGroup("TAG1");
          const QStringList childKeys = settings.childKeys();
          QHash<QString, QString> values;
          foreach (const QString &childKey, childKeys)
          values.insert(childKey, settings.value(childKey).toString());
          settings.endGroup();
          @

          P 1 Reply Last reply
          0
          • I Offline
            I Offline
            Indrajeet
            wrote on last edited by
            #12

            Hi

            How to handle if INI file is not found.

            Regards
            Indrajeet

            1 Reply Last reply
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #13

              QSettings::status

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jim_kaiser
                wrote on last edited by
                #14

                bq. How to handle if INI file is not found.

                You could handle it directly in the settings since the settings is opened in a read-write mode.

                From Documentation:

                bq. Constructs a QSettings object for accessing the settings stored in the file called fileName, with parent parent. If the file doesn't already exist, it is created.

                Now, the value() function takes a default value.
                @ QVariant value ( const QString & key, const QVariant & defaultValue = QVariant() ) const @

                So, if no setting, then your default value passed is used. This is the way to do it.

                Here's another approach just for kicks..

                From QSetting documentation, on windows, the user path seems to be
                @ %APPDATA% @
                and system path
                @ %COMMON_APPDATA% @
                Not on a windows machine right now, but you could try something like
                @
                QString iniDir = QProcessEnvironment::systemEnvironment ().value("COMMON_APPDATA")
                bool iniExists = QFileInfo(iniDir, "test.ini").exists();
                @
                Then if no settings file, instead of reading the settings.. use default settings, when closing, store the settings into the ini.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Indrajeet
                  wrote on last edited by
                  #15

                  Hi

                  I didnt get How to handle if INI file is not found.

                  Can anyone explain with example code.

                  Regards
                  Indrajeet

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jim_kaiser
                    wrote on last edited by
                    #16

                    "QSettings Constructor":http://doc.qt.nokia.com/stable/qsettings.html#QSettings-4

                    You don't need to do anything to handle missing file. If the ini filename you pass isn't present Qt will create it. So..

                    @ QSettings settings("test.ini", QSettings::IniFormat); @

                    Will automatically create the test.ini file, if it doesn't exist.

                    Now, in your code to read from settings.
                    @ foreach (const QString &childKey, childKeys)
                    values.insert(childKey, settings.value(childKey).toString()); @
                    You can pass a default argument like
                    @ values.insert(childKey, settings.value(childKey, "defaultval").toString()); @
                    So that should do for reading the settings.

                    When you write the settings back to the ini later on, you will be saving the default values you have read above since there was no ini to read from. I hope thats clear.

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Indrajeet
                      wrote on last edited by
                      #17

                      Hello jim

                      Below is my code snippet

                      @QSettings settings("test.ini", QSettings::IniFormat);
                      settings.beginGroup("TAG1");
                      const QStringList childKeys = settings.childKeys();
                      foreach (const QString &childKey, childKeys)
                      QString var = settings.value(childKey).toString();
                      settings.endGroup();@

                      You said that if test.ini is not found it will create the new by itself.
                      But wat about the contents of the file.As I am looking for TAG1 it is
                      crashing there.

                      So wat is the solution for this.

                      Regards
                      Indrajeet

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        Indrajeet
                        wrote on last edited by
                        #18

                        Hi All

                        If the INI FILE has more TAGS for Example

                        INI File Format:
                        [TAG1]
                        key1=value1
                        key2=value2
                        .
                        .
                        .
                        .
                        .
                        [TAG2]
                        key3=value3
                        key4=value4
                        .
                        .
                        .
                        .
                        How to collect values of all tags in single List and return that list.

                        Regards
                        Indrajeet

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jim_kaiser
                          wrote on last edited by
                          #19

                          I don't see why it would crash! The beginGroup simply sets the prefix to use for the keys. If your key doesn't exist
                          @ settings.value(childKey).toString() @
                          will return a empty QVariant(). You can also provide a default value like I have said above.

                          For the next question..
                          Taking code from others who have answered it above.
                          @
                          QSettings settings("test.ini", QSettings::IniFormat);
                          const QStringList childKeys = settings.allKeys();
                          QStringList values;
                          foreach (const QString &childKey, childKeys)
                          values << settings.value(childKey).toString();
                          settings.endGroup();
                          @

                          I think, you have all the info you need. Try to experiment and debug to see what is going on if there is a problem. And post the offending code.

                          V 1 Reply Last reply
                          0
                          • J jim_kaiser

                            I don't see why it would crash! The beginGroup simply sets the prefix to use for the keys. If your key doesn't exist
                            @ settings.value(childKey).toString() @
                            will return a empty QVariant(). You can also provide a default value like I have said above.

                            For the next question..
                            Taking code from others who have answered it above.
                            @
                            QSettings settings("test.ini", QSettings::IniFormat);
                            const QStringList childKeys = settings.allKeys();
                            QStringList values;
                            foreach (const QString &childKey, childKeys)
                            values << settings.value(childKey).toString();
                            settings.endGroup();
                            @

                            I think, you have all the info you need. Try to experiment and debug to see what is going on if there is a problem. And post the offending code.

                            V Offline
                            V Offline
                            victor wang
                            wrote on last edited by
                            #20

                            @jim_kaiser
                            Hi

                            This is my code here
                            And i got some error.
                            Is there anything i need to fix?

                            This is an error here

                            Please help!

                            jsulmJ 1 Reply Last reply
                            0
                            • V victor wang

                              @jim_kaiser
                              Hi

                              This is my code here
                              And i got some error.
                              Is there anything i need to fix?

                              This is an error here

                              Please help!

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #21

                              @victor-wang Why did you put ; after foreach?
                              qDebug() instead of qDebug

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • A andre

                                Perhaps, instead of storing your values in two QStringLists, you would like to store them in a single QHash<QString, QString> ?

                                That way, at least you keep your keys and values together in the same container...

                                To get them, you could modify the example code from above to something like this:
                                @
                                QSettings settings("test.ini", QSettings::IniFormat);
                                settings.beginGroup("TAG1");
                                const QStringList childKeys = settings.childKeys();
                                QHash<QString, QString> values;
                                foreach (const QString &childKey, childKeys)
                                values.insert(childKey, settings.value(childKey).toString());
                                settings.endGroup();
                                @

                                P Offline
                                P Offline
                                poojakamshetty
                                wrote on last edited by
                                #22

                                @andre Hi sir, I tried this method and I am able to read 1 tag values but if I try to read 2nd tag values I am not getting. Can you please help me with this.
                                QSettings settings("abc.ini", QSettings::IniFormat);
                                settings.beginGroup("tag 1");
                                const QStringList childKeys = settings.childKeys();
                                QHash<QString,QString>values;
                                foreach (const QString &childKey,childKeys)
                                values.insert(childKey, settings.value(childKey).toString());
                                settings.endGroup();
                                qDebug()<<values;
                                This was my code for 1 tag(Attributes)
                                but for other tag I am not getting
                                [tag 1]
                                key1=value1
                                key2=value2
                                key3=value3
                                key4=value4
                                .
                                .
                                [tag2]
                                key1=value1
                                key2=value2
                                key3=value3
                                key4=value4
                                .
                                .
                                Now how to read this tag2 I am unable to do. Can you pls tell me.
                                Thank you.

                                mrjjM 1 Reply Last reply
                                0
                                • P poojakamshetty

                                  @andre Hi sir, I tried this method and I am able to read 1 tag values but if I try to read 2nd tag values I am not getting. Can you please help me with this.
                                  QSettings settings("abc.ini", QSettings::IniFormat);
                                  settings.beginGroup("tag 1");
                                  const QStringList childKeys = settings.childKeys();
                                  QHash<QString,QString>values;
                                  foreach (const QString &childKey,childKeys)
                                  values.insert(childKey, settings.value(childKey).toString());
                                  settings.endGroup();
                                  qDebug()<<values;
                                  This was my code for 1 tag(Attributes)
                                  but for other tag I am not getting
                                  [tag 1]
                                  key1=value1
                                  key2=value2
                                  key3=value3
                                  key4=value4
                                  .
                                  .
                                  [tag2]
                                  key1=value1
                                  key2=value2
                                  key3=value3
                                  key4=value4
                                  .
                                  .
                                  Now how to read this tag2 I am unable to do. Can you pls tell me.
                                  Thank you.

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #23

                                  @poojakamshetty
                                  Hi and welcome to the forums

                                  But did you try with
                                  settings.beginGroup("tag2);
                                  and then same code otherwise ?

                                  P 1 Reply Last reply
                                  1
                                  • mrjjM mrjj

                                    @poojakamshetty
                                    Hi and welcome to the forums

                                    But did you try with
                                    settings.beginGroup("tag2);
                                    and then same code otherwise ?

                                    P Offline
                                    P Offline
                                    poojakamshetty
                                    wrote on last edited by
                                    #24

                                    @mrjj Hi sir, Yeah I tried settings.beginGroup("tag2"); I am getting the output but my key=values are not coming in order. How can I get in order?
                                    QSettings settings("abc.ini", QSettings::IniFormat);
                                    QHash<QString,QString>values;
                                    settings.beginGroup("tag1");
                                    QStringList childKeys = settings.childKeys();
                                    foreach (const QString &childKey,childKeys)
                                    values.insert(childKey, settings.value(childKey).toString());
                                    settings.endGroup();
                                    settings.beginGroup("tag2");
                                    childKeys = settings.childKeys();
                                    foreach (const QString &childKey,childKeys)
                                    values.insert(childKey, settings.value(childKey).toString());
                                    settings.endGroup();
                                    qDebug()<<"Key-value pair:"<<values;
                                    output: Key-value pair: QHash(("a2", "c2")("d", "m")("k", "v")("d2", "m2")("a", "c")("k2", "v2"))
                                    order of my mini file is
                                    [tag1]
                                    k=v
                                    a=c
                                    d=m
                                    [tag2]
                                    k2=v2
                                    a2=c2
                                    d2=m2
                                    Can you tell how to get in order

                                    jsulmJ mrjjM 2 Replies Last reply
                                    0
                                    • P poojakamshetty

                                      @mrjj Hi sir, Yeah I tried settings.beginGroup("tag2"); I am getting the output but my key=values are not coming in order. How can I get in order?
                                      QSettings settings("abc.ini", QSettings::IniFormat);
                                      QHash<QString,QString>values;
                                      settings.beginGroup("tag1");
                                      QStringList childKeys = settings.childKeys();
                                      foreach (const QString &childKey,childKeys)
                                      values.insert(childKey, settings.value(childKey).toString());
                                      settings.endGroup();
                                      settings.beginGroup("tag2");
                                      childKeys = settings.childKeys();
                                      foreach (const QString &childKey,childKeys)
                                      values.insert(childKey, settings.value(childKey).toString());
                                      settings.endGroup();
                                      qDebug()<<"Key-value pair:"<<values;
                                      output: Key-value pair: QHash(("a2", "c2")("d", "m")("k", "v")("d2", "m2")("a", "c")("k2", "v2"))
                                      order of my mini file is
                                      [tag1]
                                      k=v
                                      a=c
                                      d=m
                                      [tag2]
                                      k2=v2
                                      a2=c2
                                      d2=m2
                                      Can you tell how to get in order

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #25

                                      @poojakamshetty Please check documentation: https://doc.qt.io/qt-5/qhash.html "With QHash, the items are arbitrarily ordered". With QMap data is sorted by key.
                                      Why do you need same order in the container?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      P 1 Reply Last reply
                                      6
                                      • P poojakamshetty

                                        @mrjj Hi sir, Yeah I tried settings.beginGroup("tag2"); I am getting the output but my key=values are not coming in order. How can I get in order?
                                        QSettings settings("abc.ini", QSettings::IniFormat);
                                        QHash<QString,QString>values;
                                        settings.beginGroup("tag1");
                                        QStringList childKeys = settings.childKeys();
                                        foreach (const QString &childKey,childKeys)
                                        values.insert(childKey, settings.value(childKey).toString());
                                        settings.endGroup();
                                        settings.beginGroup("tag2");
                                        childKeys = settings.childKeys();
                                        foreach (const QString &childKey,childKeys)
                                        values.insert(childKey, settings.value(childKey).toString());
                                        settings.endGroup();
                                        qDebug()<<"Key-value pair:"<<values;
                                        output: Key-value pair: QHash(("a2", "c2")("d", "m")("k", "v")("d2", "m2")("a", "c")("k2", "v2"))
                                        order of my mini file is
                                        [tag1]
                                        k=v
                                        a=c
                                        d=m
                                        [tag2]
                                        k2=v2
                                        a2=c2
                                        d2=m2
                                        Can you tell how to get in order

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by mrjj
                                        #26

                                        @poojakamshetty
                                        Hi
                                        The out of order comes from
                                        QHash<QString, QString>values;
                                        Is what we call unordered
                                        You can use QMap instead
                                        QMap<QString, QString>values;

                                        and then it should come in the expected order.

                                        (meh i type too slow :))

                                        P 1 Reply Last reply
                                        6
                                        • jsulmJ jsulm

                                          @poojakamshetty Please check documentation: https://doc.qt.io/qt-5/qhash.html "With QHash, the items are arbitrarily ordered". With QMap data is sorted by key.
                                          Why do you need same order in the container?

                                          P Offline
                                          P Offline
                                          poojakamshetty
                                          wrote on last edited by
                                          #27

                                          @jsulm yeah understood. Thank you. But I want in the same order in the container because I can access it easily later. But in the same order, I am not getting my output

                                          JonBJ 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