Search code examples
qtqt5qmapqvariantqsettings

Why I can't store QVariantMap in QSettings?


Why it was possible in Qt 5.2 and previously and stored data in following format:

key=@Variant(\0\0\0\b\0\0\0)

but have problem now in Qt 5.11?! Following code

QVariantMap projectsMap;
for (auto project : projects)
    projectsMap.insert(key, value);

settings->setValue("Group/projects", projectsMap);

executes correctly however stores nothing to ini file.

qRegisterMetaTypeStreamOperators<QVariantMap>("QVariantMap");

does not help as well. How to store this, what is the problem here?


Solution

  • Don’t store QSettings: it’s not meant to be used that way. You should use a fresh instance of QSettings every time you change the settings. Your destructor should look as follows:

    MyClass::~MyClass() {
      QSettings s;
      s.setValue(kFoo, this->m_bar);
      …
    }
    

    QSettings is an ephemeral handle to the settings system, its instantiation is cheap. You leak it because QPointer does not destroy anything: it’s not an owning pointer.