Question

I read some QSettings from an .ini file:

QSettings* settingsDocRoot=new QSettings(_settingsFile ,QSettings::IniFormat, parent);

This is passed to some object. However, I then do a copy QSettings* s2= new QSettings(settingsDocRoot); and modify one particular value s2->setValue("path", whateverNewPath);

Basically I want to pass a slightly modified QSettings object to another object. But how do I avoid that the original ini file is updated with the changed value (s2->setValue)?

One idea was, simply to set the path to "". However, according to QSettings - where is the location of the ini file? then a default location will be assumed (OK, original file will not be changed, but unnecessary file will be written).

Was it helpful?

Solution 2

I am currently doing the following:

    QSettings* settingsWsNaviGraph = new QSettings(settingsDocRoot);
    // avoid writing to file
    settingsWsNaviGraph->setPath(QSettings::InvalidFormat, QSettings::UserScope, "");

This dirty hack seems to avoid writing, at least my original file remains unchanged and I do not see any unwanted file yet (will report if I do find one).

If this here does not work, I'll try to register my own format with bogus read/write methods. See here

OTHER TIPS

QSettings is entirely designed for persistence. If you don't want your copy to write to disk, you'd probably be better off copying all the values into a QHash and passing that to your other object:

QHash<QString, QVariant> hash;
const QStringList keys = settings->allKeys();
Q_FOREACH(QString key, keys) {
  hash[key] = settings->value(key());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top