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. How to clear current array in QSettings?
Forum Updated to NodeBB v4.3 + New Features

How to clear current array in QSettings?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.6k 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    Instead of settings.clear(); which clears the whole settings file, is there a way to only clean a certain Array?

    For example, make this:
    @[monitored_folders]
    size=1
    1\item=/usr/share/backgrounds@

    into this:
    @[monitored_folders]
    size=0@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      Just use "method setValue of class QSettings":http://qt-project.org/doc/qt-5.0/qtcore/qsettings.html#setValue and set the value of size to 0. You can find a "simple app with example usage of QSettings here":http://qt-project.org/wiki/How_to_Use_QSettings.

      http://anavi.org/

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #3

        Lets say settings files contains:

        @[monitored_folders]
        size=5
        1\item=/home/citybong/Pictures
        2\item=/home/citybong/Pictures/Animals
        3\item=/home/citybong/Pictures/Computer
        4\item=/home/citybong/Pictures/Anime & Manga
        5\item=/home/citybong/Pictures/Celebrities@

        using this code:
        @QSettings settings( "Wallch", "MainWindow" );
        settings.beginWriteArray("monitored_folders");
        settings.setValue("size", 0);
        settings.setArrayIndex(0);
        settings.setValue("item", "/usr/share/backgrounds");
        settings.endArray();
        settings.sync();@

        i though is would look like this:

        @[monitored_folders]
        size=1
        1\item=/usr/share/backgrounds@

        instead it is like this:

        @[monitored_folders]
        size=1
        1\item=/usr/share/backgrounds
        2\item=/home/citybong/Pictures
        3\item=/home/citybong/Pictures/Animals
        4\item=/home/citybong/Pictures/Computer
        5\item=/home/citybong/Pictures/Anime & Manga
        6\item=/home/citybong/Pictures/Celebrities@

        which eventually works because size=1, but why is this happening?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          You have to use "remove":http://qt-project.org/doc/qt-4.8/qsettings.html#remove with a group (see the second example)

          Or the example below:
          @
          #include <QSettings>
          #include <QList>
          #include <QStringList>
          #include <QDebug>

          struct Login {
          QString userName;
          QString password;
          };

          int main(int argc, char *argv[])
          {
          QList<Login> logins;
          for ( int i = 0; i < 10; ++i )
          {
          Login lgin;
          QString name ( "Name%1");
          lgin.userName = name.arg(i);
          name = "Pass%1";
          lgin.password = name.arg(i);
          logins.push_back ( lgin );
          }

          QSettings settings ("c:/Source/TestQSettings/test.ini", QSettings::IniFormat );
          
          settings.beginWriteArray("logins");
          for (int i = 0; i < logins.size(); ++i) {
              settings.setArrayIndex(i);
              settings.setValue("userName", logins.at(i).userName);
              settings.setValue("password", logins.at(i).password);
          }
          settings.endArray();
          
          settings.beginGroup ( "AnotherGroup");
          settings.setValue ( "test", 10 );
          settings.endGroup ();
          
          QStringList sl = settings.allKeys ();
          qDebug() << sl;
          
          settings.beginGroup ("logins");
          settings.remove ("");
          settings.endGroup ();
          
          sl = settings.allKeys();
          qDebug() << sl;
          
          return 0;
          

          }
          @

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            I have added a doc note to "Qt4.8 documentation":http://qt-project.org/doc/qt-4.8/qsettings.html I would assume it is also working for Qt5. I appreciate your feedback when tested in Qt5. I would copy then to Qt 5 as well. Thanks .

            BTW The example does not fulfill complete your request.
            In order to get what you want to have, you can add after removing the group:
            @
            settings.beginWriteArray("logins", 0);
            settings.endArray ();
            @
            Or just writing the updated array to settings.

            Vote the answer(s) that helped you to solve your issue(s)

            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