Question

I am using QSettings to read key/value pairs from an INI file (must be INI format). I have a case where I have to store zero or more key/value pairs with the same key; eg:

[users]
user=bob
user=steve
user=frank

Is this acceptable to QSettings? How would I retrieve all these key/value pairs using QSettings? (will it return all as a QString list?)

Was it helpful?

Solution

It's probably a little late, but I think it would help someone else.

I think you have to use "array" :

see : http://qt-project.org/doc/qt-4.8/qsettings.html#beginReadArray and http://qt-project.org/doc/qt-4.8/qsettings.html#beginWriteArray

You have to write something like that :

[users]
user/size=3
user/1/name=bob
user/2/name=steve
user/3/name=frank

and to parse it :

QSetting settings("file.ini", QSettings::IniFormat);
settings.beginGroup("users");
int size = settings.beginReadArray("user");
for (int i = 0; i < size; ++i) {
    settings.setArrayIndex(i);
    QString user = settings.value("name").toString();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top