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 array of values (rather than groups)
Forum Updated to NodeBB v4.3 + New Features

QSettings array of values (rather than groups)

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.4k Views 3 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.
  • M Offline
    M Offline
    mattfbacon
    wrote on last edited by
    #1

    QSettings has support for arrays, as shown in the documentation. Assuming INI format, an array might expand to something like:

    [foo]
    size=2
    [foo/0]
    bar=3
    baz=4
    [foo/1]
    bar=5
    baz=6
    

    which is perfectly acceptable, but I want to know if I could make something like the following:

    [foo]
    size=2
    0=1
    1=2
    

    that is, an array of values rather than groups of values.

    If this is not possible I will just make something like:

    [foo/0]
    value=1
    [foo/1]
    value=2
    

    or write some functions to manually put the size and items, but I would rather not do that if possible.

    Is what I'm describing possible? I've tried passing an empty string to QSettings::value but I get an error.

    Pl45m4P 1 Reply Last reply
    0
    • M mattfbacon

      QSettings has support for arrays, as shown in the documentation. Assuming INI format, an array might expand to something like:

      [foo]
      size=2
      [foo/0]
      bar=3
      baz=4
      [foo/1]
      bar=5
      baz=6
      

      which is perfectly acceptable, but I want to know if I could make something like the following:

      [foo]
      size=2
      0=1
      1=2
      

      that is, an array of values rather than groups of values.

      If this is not possible I will just make something like:

      [foo/0]
      value=1
      [foo/1]
      value=2
      

      or write some functions to manually put the size and items, but I would rather not do that if possible.

      Is what I'm describing possible? I've tried passing an empty string to QSettings::value but I get an error.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @mattfbacon

      You shouldn't worry about how it is written, when you read the file with the same program again. Do you want to read this file with another program?

      To write arrays, there is beginWriteArray and beginReadArray

      • https://doc.qt.io/qt-5/qsettings.html#beginWriteArray
      • https://doc.qt.io/qt-5/qsettings.html#beginReadArray

      Documentation says:

      The generated keys will have the form

      logins/size
      logins/1/userName
      logins/1/password
      logins/2/userName
      logins/2/password
      logins/3/userName
      logins/3/password
      

      Which is close to what you want, right?

      Edit:

      Just remembered this topic

      Btw: The examples in the documentation of QSettings are pretty messed up :)
      The internal stuff that QSettings does seems to be a bit different from what is in the documentation... but as long as it works...

      See: https://bugreports.qt.io/browse/QTBUG-81951

      ... and a typo / mistake in the examples made it even to the Qt6 documentation :)
      https://doc-snapshots.qt.io/qt6-dev/qsettings.html#beginWriteArray
      list.at(i) instead of logins.at(i)
      In the code sample there is no list ;-)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Christian EhrlicherC M 2 Replies Last reply
      0
      • Pl45m4P Pl45m4

        @mattfbacon

        You shouldn't worry about how it is written, when you read the file with the same program again. Do you want to read this file with another program?

        To write arrays, there is beginWriteArray and beginReadArray

        • https://doc.qt.io/qt-5/qsettings.html#beginWriteArray
        • https://doc.qt.io/qt-5/qsettings.html#beginReadArray

        Documentation says:

        The generated keys will have the form

        logins/size
        logins/1/userName
        logins/1/password
        logins/2/userName
        logins/2/password
        logins/3/userName
        logins/3/password
        

        Which is close to what you want, right?

        Edit:

        Just remembered this topic

        Btw: The examples in the documentation of QSettings are pretty messed up :)
        The internal stuff that QSettings does seems to be a bit different from what is in the documentation... but as long as it works...

        See: https://bugreports.qt.io/browse/QTBUG-81951

        ... and a typo / mistake in the examples made it even to the Qt6 documentation :)
        https://doc-snapshots.qt.io/qt6-dev/qsettings.html#beginWriteArray
        list.at(i) instead of logins.at(i)
        In the code sample there is no list ;-)

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

        @Pl45m4 said in QSettings array of values (rather than groups):

        The internal stuff that QSettings does seems to be a bit different from what is in the documentation...

        It's not - how the backend does write out the data is not specified (and can't since e.g. the registry is other than a plain ini file)

        ... and a typo / mistake in the examples made it even to the Qt6 documentation :)

        How about providing a patch instead?

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

        Pl45m4P 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @Pl45m4 said in QSettings array of values (rather than groups):

          The internal stuff that QSettings does seems to be a bit different from what is in the documentation...

          It's not - how the backend does write out the data is not specified (and can't since e.g. the registry is other than a plain ini file)

          ... and a typo / mistake in the examples made it even to the Qt6 documentation :)

          How about providing a patch instead?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Christian-Ehrlicher said in QSettings array of values (rather than groups):

          How about providing a patch instead?

          Where? :)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          SGaistS 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @Christian-Ehrlicher said in QSettings array of values (rather than groups):

            How about providing a patch instead?

            Where? :)

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

            @Pl45m4 hi,

            You can start from here.

            I know it may look a bit daunting at first but it's not that much in the end :-)

            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
            0
            • M Offline
              M Offline
              mattfbacon
              wrote on last edited by
              #6

              Haha, @Christian-Ehrlicher , "submit a patch" seems to be your go-to advice :)

              I have no need to do so. As I said, I can work around this.

              I was just wondering if there was a way to do what I'm trying to do.

              1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @mattfbacon

                You shouldn't worry about how it is written, when you read the file with the same program again. Do you want to read this file with another program?

                To write arrays, there is beginWriteArray and beginReadArray

                • https://doc.qt.io/qt-5/qsettings.html#beginWriteArray
                • https://doc.qt.io/qt-5/qsettings.html#beginReadArray

                Documentation says:

                The generated keys will have the form

                logins/size
                logins/1/userName
                logins/1/password
                logins/2/userName
                logins/2/password
                logins/3/userName
                logins/3/password
                

                Which is close to what you want, right?

                Edit:

                Just remembered this topic

                Btw: The examples in the documentation of QSettings are pretty messed up :)
                The internal stuff that QSettings does seems to be a bit different from what is in the documentation... but as long as it works...

                See: https://bugreports.qt.io/browse/QTBUG-81951

                ... and a typo / mistake in the examples made it even to the Qt6 documentation :)
                https://doc-snapshots.qt.io/qt6-dev/qsettings.html#beginWriteArray
                list.at(i) instead of logins.at(i)
                In the code sample there is no list ;-)

                M Offline
                M Offline
                mattfbacon
                wrote on last edited by
                #7

                @Pl45m4 I realize now that what I'm trying to do doesn't make much sense because even if I could write what I want to the settings, I would have no good way to read it. I'll just use a workaround.

                Pl45m4P 1 Reply Last reply
                0
                • M mattfbacon

                  @Pl45m4 I realize now that what I'm trying to do doesn't make much sense because even if I could write what I want to the settings, I would have no good way to read it. I'll just use a workaround.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @mattfbacon

                  The "provide a patch" was directed to me :)
                  Because the QSetting documentation has a mistake


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  M 1 Reply Last reply
                  1
                  • Pl45m4P Pl45m4

                    @mattfbacon

                    The "provide a patch" was directed to me :)
                    Because the QSetting documentation has a mistake

                    M Offline
                    M Offline
                    mattfbacon
                    wrote on last edited by
                    #9

                    @Pl45m4 Ah, alright. I will probably overload QDataStream& operator<< and operator>> for QVector<T> where it just puts the size and then the items.

                    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