Horst Walter
Horst Walter

Reputation: 14081

QSettings: Copy, modify but avoid changing underlying .ini file

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

Upvotes: 2

Views: 3636

Answers (2)

Horst Walter
Horst Walter

Reputation: 14081

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

Upvotes: 2

Dan Milburn
Dan Milburn

Reputation: 5718

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());
}

Upvotes: 3

Related Questions