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.9k 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.
  • 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
            • mrjjM mrjj

              @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 Offline
              P Offline
              poojakamshetty
              wrote on last edited by
              #28

              @mrjj I tried this but I am not getting my output as expected order.

              1 Reply Last reply
              0
              • P poojakamshetty

                @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 Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #29

                @poojakamshetty

                But in the same order, I am not getting my output

                I wonder what that means? Have you changed QHash over to QMap? QMap is ordered, but only by key name (i.e. alphabetically). If you retrieve them from the file in a particular order, you will have to use a list/array instead if you wish to retain that particular order.

                P 1 Reply Last reply
                2
                • JonBJ JonB

                  @poojakamshetty

                  But in the same order, I am not getting my output

                  I wonder what that means? Have you changed QHash over to QMap? QMap is ordered, but only by key name (i.e. alphabetically). If you retrieve them from the file in a particular order, you will have to use a list/array instead if you wish to retain that particular order.

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

                  @JonB Got it,thank you. I am getting in alphabetical order when I am using QMap. My Requirement is to get in the order the same as my .ini file. What should I do?

                  mrjjM 1 Reply Last reply
                  0
                  • P poojakamshetty

                    @JonB Got it,thank you. I am getting in alphabetical order when I am using QMap. My Requirement is to get in the order the same as my .ini file. What should I do?

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

                    @poojakamshetty
                    hi
                    Well store them in a list instead. that will store them as added but you lose
                    the ability to lookup by key but maybe thats fine.

                    P 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @poojakamshetty
                      hi
                      Well store them in a list instead. that will store them as added but you lose
                      the ability to lookup by key but maybe thats fine.

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

                      @mrjj Hi,
                      If I store them in a list I think I won't get the output as key-value pair right?

                      mrjjM 1 Reply Last reply
                      0
                      • P poojakamshetty

                        @mrjj Hi,
                        If I store them in a list I think I won't get the output as key-value pair right?

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

                        @poojakamshetty
                        Hi
                        Not if you only store a string but you could easily just do
                        QVector<QPair<QString, QString>> list;
                        or a similar structure to store both key and value in the same list.

                        P 2 Replies Last reply
                        2
                        • mrjjM mrjj

                          @poojakamshetty
                          Hi
                          Not if you only store a string but you could easily just do
                          QVector<QPair<QString, QString>> list;
                          or a similar structure to store both key and value in the same list.

                          P Offline
                          P Offline
                          poojakamshetty
                          wrote on last edited by
                          #34
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @poojakamshetty
                            Hi
                            Not if you only store a string but you could easily just do
                            QVector<QPair<QString, QString>> list;
                            or a similar structure to store both key and value in the same list.

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

                            @mrjj Hi
                            Okay, Got it Thank you.
                            I have a doubt. In the container, the values can be stored in any order or if they are not in the order as .ini file will that be any problem later?

                            mrjjM JonBJ 2 Replies Last reply
                            0
                            • P poojakamshetty

                              @mrjj Hi
                              Okay, Got it Thank you.
                              I have a doubt. In the container, the values can be stored in any order or if they are not in the order as .ini file will that be any problem later?

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

                              @poojakamshetty
                              Hi
                              They will be stored in same order as you add them so if you add them in the order you read them it should be as you want. (same order as in ini file)

                              P 1 Reply Last reply
                              1
                              • P poojakamshetty

                                @mrjj Hi
                                Okay, Got it Thank you.
                                I have a doubt. In the container, the values can be stored in any order or if they are not in the order as .ini file will that be any problem later?

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

                                @poojakamshetty

                                if they are not in the order as .ini file will that be any problem later?

                                The ini file/QSettings does not care about the order of entries. Only you seem to want this:

                                My Requirement is to get in the order the same as my .ini file.

                                Why do you even need this?

                                1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  @poojakamshetty
                                  Hi
                                  They will be stored in same order as you add them so if you add them in the order you read them it should be as you want. (same order as in ini file)

                                  P Offline
                                  P Offline
                                  poojakamshetty
                                  wrote on last edited by
                                  #38
                                  This post is deleted!
                                  JonBJ 1 Reply Last reply
                                  0
                                  • P poojakamshetty

                                    This post is deleted!

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

                                    @poojakamshetty
                                    It helps if you use code tags for posting this here.

                                    values.insert(childKey, settings.value(childKey).toString());

                                    Don't you mean:

                                    QVector<QPair<QString, QString>> values;
                                    ...
                                    values.insert(i, QPair<QString, QString>(childKey, settings.value(childKey).toString()));
                                    

                                    ?

                                    Define yourself a type for QPair<QString, QString> and this will read easier.

                                    P 1 Reply Last reply
                                    2
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #40

                                      Hi,

                                      Since you really want a specific order, wouldn't it be simpler to sort your container after loading it with data ? That way you would be a bit less depending on the storage format.

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

                                      mrjjM 1 Reply Last reply
                                      0
                                      • SGaistS SGaist

                                        Hi,

                                        Since you really want a specific order, wouldn't it be simpler to sort your container after loading it with data ? That way you would be a bit less depending on the storage format.

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

                                        @SGaist
                                        Hi
                                        He wants same order as in the ini file so not sure we can sort it out by sorting ;)
                                        Unless you have something magic in mind?

                                        SGaistS 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @SGaist
                                          Hi
                                          He wants same order as in the ini file so not sure we can sort it out by sorting ;)
                                          Unless you have something magic in mind?

                                          SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #42

                                          @mrjj said in Regarding Using QSettings to read INI File:

                                          @SGaist
                                          Hi
                                          He wants same order as in the ini file so not sure we can sort it out by sorting ;)
                                          Unless you have something magic in mind?

                                          Nothing magic, but if order is important then maybe sorting the container be a better idea than relying on something that can be modified with a text editor.

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

                                          mrjjM 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