Problem after adding of new key QSetting



  • Hy Folks,

    I have a project that uses QSettings instance to store the user custom settings. Recently I have the following .ini file which I manage via a resource file:

    @
    [default]
    client\channelColor\1\color=#55E411
    client\channelColor\2\color=#2D41EA
    client\channelColor\3\color=#FF0000
    client\channelColor\4\color=#F8B80D
    client\channelColor\size=4
    client\channelDecode\1\decode=<NONE>
    client\channelDecode\size=1
    client\channelState\1\state=AC
    client\channelState\2\state=DC
    client\channelState\3\state=GND
    client\channelState\size=3
    general\Connection\connectionCount=4
    general\Connection\serverport=50000
    @

    When my manager class starts, I instantiate a QSettings object (_sessions):
    @
    sessionManager::sessionManager(): _sessions(":/general/res/sessions.ini", QSettings::IniFormat) {
    _session.clear();
    _cache.clear();
    }
    @

    The problem occures when I have to copy the whole group of "[default]" into another. For that I use the following code:
    @
    QMap<QString, QVariant> tmpContainer;
    _sessions.beginGroup("default");
    for(const QString& key: _sessions.allKeys()) {
    tmpContainer[key] = _sessions.value(key);
    }

    _sessions.endGroup();
    _sessions.beginGroup(session);
    for(QMap<QString, QVariant>::iterator it = tmpContainer.begin(); it != tmpContainer.end(); ++it) {
        _sessions.setValue(session + it.key(), it.value());
    }
    
    _sessions.endGroup();
    

    @

    The required behavioral of the above code:

    • set the group to "default"
    • instantiate a QMap that will store the keys and values ofrom the "default" group and fill it up
    • close the group and open another
    • the second cycle write the keys and values into the opened group.

    After the running of the program, I open my .ini file but nothing happened... :(
    Can anybody help me?

    Regards,
    Norbert


  • Lifetime Qt Champion

    Hi,

    Qt resources file are read-only so all your writing to this file will fail.

    You have two solutions there:

    Copy the ini file on the file system somewhere suitable using e.g.e QDesktopServices(Qt 4) or QStandardPaths (Qt 5) with DataLocation and use that one

    Create the file from scratch at application startup (also using a suitable path)

    Hope it helps


Log in to reply
 

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