Storing and Managing Application Settings

  • Hello, I'm developing a Qt application and, having reached a quite advanced stage, I feel the need of a good settings management system.

    While I have no problem designing the settings window Ui, I need some advice designing the "settings management" part. This is the first large application I design with qt and I have very little idea of how settings should be stored, loaded and how default values should be kept in memory.
    Of course I've already read the manual and searched the forum but I didn't find anything similar to my question.

    I have a set of variables (strings, numerical, Qt-data-types, etc) which the user should be allowed to edit according to his preferences. I could use a QSettings object and keep these values saved.

    But there are some complications. As you can see in most advanced/well-built softwares, some settings can take effect immediately, some of them should - or must absolutely - take effect after a restart (otherwise things gets messed up). How can I deal with this?
    Also, when I start the program for the very first time, when a user wants to, or when he messes up with the config file, it should be possible to use a set of default values. What could be a good solution to "ship" my software with these configs?
    Some config values are absolutely needed for the program to run, in case a settings is missing, a I absolutely need to fall back to a "backup default value".

    Of course I've done my homework before asking here and I've come up with an idea. I think it's a quite awful one but haven't found any other.

    My idea was to create a "settingsManager" object which hides all the details of settings management inside.
    It could be something like this (pseudocode):

    class SettingsManager
    QSettings immediateDefaultSettings; // Contains default app settings
    // which apply immediately
    QSettings restartDefaultSettings; // Contains default app settings
    // which apply after an app restart

    QSettings immediateSettings; // Contains settings which apply imm.
    QSettings restartSettings; // Contains settings which apply after
    // an app restart

    setValue(key, value); // Store a value
    getValue(key); // Retrieve a stored value

    // NOTES: defaultSettings does not open a file but
    // it's initialized with SettingsManager instance
    // and contains all default values. immmediateSettings
    // and restartSettings load their own .ini file
    // (2 separate files) and their content is checked against
    // defaults. When a key is added, SettingsManager
    // automatically finds out whether it's an "immediate" or
    // "restart" settings and store in the appropriate member.
    // Reading is similar.


    Of course such an implementation would require 4 different objects and a lot of useless initialisations. There's no need to create and populate a QSettings object containing defaults values at each application start, since they never change they could be set at compile time. Putting everything in a single file and dividing it in sections could be better, but I still find it a bad idea.

    I find QSettings quite limiting since it can only manipulate a simple "key" -> value associative array.
    How do you usually approach to a problem like this? Any suggestion is welcome and really appreciated!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can use dedicated objects that are the interface between your setting storage and your application. These take care of returning a default value if none is available or cache a value that needs a restart to take effect. They also let you just use the settings rather than handle them in each of your class separately.

    Hope it helps

  • Good! I was even lazier. I just created a global object "Settings" that is created on startup and manage QSettings load/save, I load the QSettings variable to public field.

    Your idea sounds good, but the getters/setters can really become hard to maintain. I come from Java background, all the teacher always say to use getters/setter, but it feel like a waste of time sometimes..

    Good luck!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.