Search code examples
c++qtqsettings

Possible to store some settings per launch (but reset between launches) using QSettings or other classes?


I have a Qt application where I'm already using QSettings to store persistent state between launches. However, there are some state-like things that I would like to store only as long as the current session is valid, and I would not like them to persist between different launches of the application.

Is there an option of QSettings that I'm missing -- or perhaps some other Qt-based solution to this? Or am I basically stuck rolling my own? (In the form of a static std::hash_map or something, I imagine.)


Solution

  • One option can be to use a temporary file (QTemporaryFile is a convenient way to do it) to store the session settings, so it is automatically destroyed when you close de application (or the session, just closing both settings and temporary file):

    QTemporaryFile tmpFile;
    tmpFile.open();
    QSettings sessionSettings(tmpFile.fileName(), QSettings::IniFormat);
    

    Just store both the temporary file and the settings together so they have the same life-span.

    Two comments on it: be aware that QTemporaryFile::fileName() returns an empty string until open is called. Also, you will have to use a file-based settings' format like INI or similar.