How to clear current array in QSettings?



  • 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@



  • 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.



  • 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?


  • Moderators

    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;
    

    }
    @


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.