Toolbox Qt
Loading...
Searching...
No Matches
tbq::SettingsIni Class Referencefinal

Class used to manage INI configuration file. More...

Public Types

using CbHook = std::function<bool(const QFileInfo &fileInfo)>
 Custom callback hook use to implement custom behaviour.
 

Public Member Functions

bool loadSettings (const QFileInfo &fileInfo)
 Load settings from INI configuration file.
 
QFileInfo getPath () const
 
void groupBegin (TOOLBOXQT_QTCOMPAT_STR_VIEW keyGroup)
 
void groupEnd ()
 
void setValue (TOOLBOXQT_QTCOMPAT_STR_VIEW key, const QVariant &value)
 
QVariant getValue (TOOLBOXQT_QTCOMPAT_STR_VIEW key, const QVariant &defaultValue=QVariant()) const
 
void setHooksPreLoadSettings (CbHook hookPreload)
 Use to set custom behaviour before loading settings.
 
void setHooksPostLoadSettings (CbHook hookPostload)
 Use to set custom behaviour after loading settings.
 

Detailed Description

Class used to manage INI configuration file.

This class allow to easily manage settings depending on a .ini file.
It will allow for example to not have to remember path of configuration file each time we need it !

Note
Don't use this class if INI format is not mandatory, QSettings already provide a way to manage other format without settings parameters each time at: https://doc.qt.io/qt-6/qsettings.html#basic-usage

To use this class in a project, it will be easier to create a singleton from it adpated to our specific configuration file.
This class also allow to have a custom behaviour for pre and post load operations of the configuration file via setHooksPreLoadSettings() and setHooksPostLoadSettings().
We can defines custom ones like this:

  • Header file:
    1#ifndef PREFERENCES_H
    2#define PREFERENCES_H
    3
    4#include "toolboxqt/core/settingsini.h"
    5
    6#include <QVersionNumber>
    7
    8#define mPrefs (Preferences::instance())
    9
    10class Preferences final : public QObject
    11{
    12 Q_OBJECT
    13 APP_DISABLE_COPY_MOVE(Preferences)
    14
    15public:
    16 static Preferences& instance();
    17
    18public:
    19 void setup(const QFileInfo &fileInfo);
    20
    21private:
    22 Preferences();
    23
    24private:
    25 bool preLoadSettings(const QFileInfo &fileInfo);
    26 bool postLoadSettings(const QFileInfo &fileInfo);
    27
    28private:
    29 tbq::SettingsIni m_settings;
    30
    31private:
    32 static const QVersionNumber SUPPORT_VERSION;
    33};
    34
    35#endif // PREFERENCES_H
    Class used to manage INI configuration file.
    Definition settingsini.h:15
  • Source file:
    1#include "preferences.h"
    2
    3#include "toolboxqt/core/corehelper.h"
    4
    5const QVersionNumber Preferences::SUPPORT_VERSION = QVersionNumber(1, 0, 0);
    6
    7Preferences& Preferences::instance()
    8{
    9 static Preferences instance;
    10 return instance;
    11}
    12
    13Preferences::Preferences()
    14{
    15 /* Nothing to do */
    16}
    17
    18void Preferences::setup(const QFileInfo &fileInfo)
    19{
    20 // Here we use "std::bind", but we could have also used a lambda function instead
    21 m_settings.setHooksPreLoadSettings(std::bind(&Preferences::preLoadSettings, this, std::placeholders::_1));
    22 m_settings.setHooksPostLoadSettings(std::bind(&Preferences::postLoadSettings, this, std::placeholders::_1));
    23
    24 m_settings.loadSettings(fileInfo);
    25}
    26
    27bool Preferences::preLoadSettings(const QFileInfo &fileInfo)
    28{
    29 /* Verify that configuration file exists */
    30 const QString filePath = fileInfo.absoluteFilePath();
    31 if(!QFile::exists(filePath)){
    32 const QString err = QString("Could not find configuration file: %1").arg(filePath);
    34 return false;
    35 }
    36
    37 return true;
    38}
    39
    40bool Preferences::postLoadSettings(APP_VAR_UNUSED const QFileInfo &fileInfo)
    41{
    42 static const QString KEY_VERSION_FILE = "app/version_cfg_file";
    43
    44 /* Verify configuration version compatibility */
    45 const QString semverStr = m_settings.getValue(KEY_VERSION_FILE).toString();
    46 const QVersionNumber semver = QVersionNumber::fromString(semverStr);
    47
    48 if(semver.majorVersion() < SUPPORT_VERSION.majorVersion() || semver.majorVersion() > SUPPORT_VERSION.majorVersion()){
    49 QString err = QString("Unable to load configuration file of application, version is unsupported [read: %1, major-supported: %2]")
    50 .arg(semver.toString()).arg(SUPPORT_VERSION.majorVersion());
    51
    53 return false;
    54 }
    55
    56 /* Update file settings properties */
    57 m_settings.setValue(KEY_VERSION_FILE, SUPPORT_VERSION.toString());
    58
    59 return true;
    60}
    static void quitApplication(const QString &reason=QString(), QWidget *parent=nullptr)
    Use to quit application.
    Definition corehelper.cpp:56

Then we only have to initialize it in our main:

1#include "mainwindow.h"
2
3#include <QApplication>
4
5#include "preferences.h"
6
7/*****************************/
8/* Macro definitions */
9/*****************************/
10#define APP_CFG_FILE "configurations/configuration.ini"
11
12/*****************************/
13/* Main method */
14/*****************************/
15int main(int argc, char *argv[])
16{
17 /* Manage application properties */
18 QApplication app(argc, argv);
19
20 /* Set application configuration file */
21 Preferences::instance().setup(QFileInfo(APP_CFG_FILE));
22
23 /* Create main window */
24 MainWindow mainWindow;
25 mainWindow.show();
26
27 return app.exec();
28}

Then we can use it anywhere with:

mPrefs.getValue("mySection/myKey");

Member Typedef Documentation

◆ CbHook

using tbq::SettingsIni::CbHook = std::function<bool(const QFileInfo &fileInfo)>

Custom callback hook use to implement custom behaviour.

Parameters
[in]fileInfoPath to configuration file used with loadSettings()
Note
Defining custom hook is not mandatory, default are provided (they do nothing excepting returning true).
Returns
Must return true if succeed.
See also
setHooksPreLoadSettings(), setHooksPostLoadSettings()

Member Function Documentation

◆ loadSettings()

bool tbq::SettingsIni::loadSettings ( const QFileInfo & fileInfo)

Load settings from INI configuration file.

Parameters
fileInfoINI configuration file to use
Returns
Return true if loading succeed.
See also
setHooksPreLoadSettings(), setHooksPostLoadSettings()

◆ setHooksPreLoadSettings()

void tbq::SettingsIni::setHooksPreLoadSettings ( CbHook hookPreload)

Use to set custom behaviour before loading settings.

Parameters
hookPreloadCustom callback to use
See also
setHooksPostLoadSettings()

◆ setHooksPostLoadSettings()

void tbq::SettingsIni::setHooksPostLoadSettings ( CbHook hookPostload)

Use to set custom behaviour after loading settings.

Parameters
hookPostloadCustom callback to use
See also
setHooksPreLoadSettings()

The documentation for this class was generated from the following files: