Application settings with QSettings
Some of the application settings should be persistent between shutdowns and starts of the application. QSettings class from Qt library is very easy and convenient way to achieve it with just a few lines of code.
The following example shows how to do it.
- Set the application name, the organization name and domain for your application.
These names will be used to create unique application settings file. Apply the names to the QApplication object, like in the example below. This is not a necessary step, but if you skip it you will need to pass application name and organization name every time you call the QSettings constructor. With this step, you are able to call QSettings constructor with no parameters from everywhere inside your application code. - Define which parameters should be persistent.
For example, in a minesweeper game, it's nice to store the user configuration of mine-board: the width and height and the mines count. These params can be encapsulated in a structure: This structure can be a private member of MainWindow of application. When the game is (re)started, the values from that structure are used to create the new board. - Create loadSettings and saveSettings functions.
Create a function loadSettings to run at the application startup and saveSettings to run before the application is closed. LoadSettings function loads saved settings or uses default values if none settings were stored. The settings are read with the usage ofQSettings::value(const QString & key, const QVariant & defaultValue)
method. In the following example, all default values are equal 10. Note: QSettings store the settings as the key-value pairs. The key is the QString defining the name of the variable (here: "height", "width" and "mines"). The value is stored as QVariant, that's why .toInt() call is necessary.
Storing the settings with QSettings is also very straightforward. The saveSettings function should useQSettings::setValue(const QString & key, const QVariant & value)
method: The saveSettings function should be called when the user decided to close the program. There is no point in saving them earlier; as long as the application runs, they can be changed multiple times. When the application is being closed we can be sure that the settings will not be changed in this lifecycle anymore. Overwrite the closeEvent for the MainWindow and call the saveSettings from that point: The job is done! This minimal example provided the whole implementation for basic use of QSettings in the application.
Where is the config file?
If you don’t set the file location (what can be done with the use of special QSettings constructor), the default location is used. The default location depends on your OS (Locations where application settings are stored). You can also check the file location by calling QSettings::fileName()
method.
What’s inside the config file?
As it was mentioned by the LoadSettings topic, the configuration file stores the keys as QStrings, and the values as QVariants. Let’s take a look how it looks like in practice. The config file made in this example contains text:
More info
For more detailed information, refer to Qt docs: QSettings. If you want to see QSettings in action, you can check out my Minesweeper game on GitHub. The examples in this post are coming from this repo.