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.

  1. 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.
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        a.setOrganizationName("MyOrganization");
        a.setOrganizationDomain("MyDomain");
        a.setApplicationName("MyAppName");
    
        MainWindow window;
        window.show();
        return a.exec();
    }
    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.

  2. 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:
    struct SPreferences
    {
        int32_t width;
        int32_t height;
        int32_t mines;
    };
    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.

  3. Create loadSettings and saveSettings functions.
    Create a function loadSettings to run at the application startup and saveSettings to run before the application is closed.
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        /* ... */
    private:
    
        void loadSettings();
        void saveSettings();
    
        SPreferences m_prefs;
    };
    LoadSettings function loads saved settings or uses default values if none settings were stored. The settings are read with the usage of QSettings::value(const QString & key, const QVariant & defaultValue) method. In the following example, all default values are equal 10.
    void MainWindow::loadSettings()
    {
      QSettings settings;
      m_prefs.width = settings.value("width", 10).toInt();
      m_prefs.height = settings.value("height", 10).toInt();
      m_prefs.mines = settings.value("mines", 10).toInt();
    }
    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 use QSettings::setValue(const QString & key, const QVariant & value) method:
    void MainWindow::saveSettings()
    {
        QSettings settings;
        settings.setValue("width", m_prefs.width);
        settings.setValue("height", m_prefs.height);
        settings.setValue("mines", m_prefs.mines);
    }
    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:
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        void closeEvent(QCloseEvent *event);
    private:
        void loadSettings();
        void saveSettings();
    
        SPreferences m_prefs;
    };
    void MainWindow::closeEvent(QCloseEvent *event)
    {
        saveSettings();
        event->accept();
    }
    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:

[General]
height=14
mines=10
width=14

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.