Settings Dialog with QSettings

  • Hi,

    currently I'm implementing a settings dialog into my small appilcation. I found the "Config Dialog Example" from Qt and implemented it into my program. It's working fine, but has no "logic" behind the gui.

    Now I want to read and write the settings from different places and files in the source code (not only in the config dialog). I'm using the QSettings class and save my parameters in an ini-file.

    What is the best way to access the file? Should I load it once at application startup and give it as parameter to the config file class or should I load it every time, when I need one parameter?

    Thank you!

    edit: Or is it maybe the best way to implement a settings handler class and access it in every class I will read and write parameter to the one file?

  • HI!

    I'm load needed ini file every time I'm want to read or write parameter(s).
    Why hold opened descriptor whole time program in work?

    It's first.

    the second. If your ini file have been changed outside program you can't see this until restart program if you open file once at start up program.

  • @Subst27
    Okay, good reason. I think I will implement it this way and give the name of the file as parameter. Thank you:)

  • I use separate class and some static methods in it

    such as
    @static void putSetting(QString iniFile,QString group,QString key,QVariant value);
    static QVariant getSetting(QString iniFile,QString group,QString key,QVariant defaultValue);@

  • Can you provide me the implementation ot these two methods also, please? I don't know how to handle the group.

  • sure.

    @void TCommon::putSetting(QString iniFile,QString group,QString key,QVariant value)
    QSettings settings(/QSettings::IniFormat,QSettings::UserScope,""some",iniFile.baseName());///iniFile,QSettings::IniFormat);
    QVariant TCommon::getSetting(QString iniFile,QString group,QString key,QVariant defaultValue)
    QSettings settings(/QSettings::IniFormat,QSettings::UserScope,"some",iniFile.baseName());///iniFile,QSettings::IniFormat);
    QVariant value=settings.value(key,defaultValue);
    return value;

    Note. This methods I use for windows and linux only. For more general purpose I use another implementation (like commented in this code).

    It's because on Mac and android other structure of deployment

  • Works perfectly, thanks a lot!:)

  • I am using a single QObject-derived class as my settings class (well, per library that is). That class is the one and only accesspoint to the settings, and settings are exposed as Q_PROPERTYs (with getter, setter, change signal).

    That central settings class manages its settings via a QSettings instance (well, it's a bit more complicated in our case, but that doesn't matter for the argument).

    Note that I do not allow any key-based access to any settings value from outside of this settings class. Also: no QVariant based values: everything is strongly typed. That means that if you have an enum that encodes some possible values, you can just set this enum and get it too, not an int.

Log in to reply

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