AberrantWolf
AberrantWolf

Reputation: 506

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

Upvotes: 2

Views: 473

Answers (2)

elcuco
elcuco

Reputation: 9208

Why using QSettings then? instead you are looking into a normal shared (singleton?) hashmap?

Quoting https://doc.qt.io/qt-5/qsettings.html#details :

If all you need is a non-persistent memory-based structure, consider using QMap instead.

Upvotes: 3

cbuchart
cbuchart

Reputation: 11575

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.

Upvotes: 4

Related Questions