Toolbox Qt
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
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 (TB_QTCOMPAT_STR_VIEW keyGroup)
 
void groupEnd ()
 
void setValue (TB_QTCOMPAT_STR_VIEW key, const QVariant &value)
 
QVariant getValue (TB_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.
 

Static Public Member Functions

static SettingsIniinstance ()
 

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 !
This class used the singleton pattern, to use it, we can use:

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, we only have to initialize it in main method:

1#include "mainwindow.h"
2
3#include <QApplication>
4
5#include "toolboxqt/core/settingsini.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 mSettings.loadSettings(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:

mSettings.getValue("mySection/myKey");

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:

Member Typedef Documentation

◆ CbHook

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()

◆ setHooksPostLoadSettings()

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

Use to set custom behaviour after loading settings.

Parameters
hookPostloadCustom callback to use
See also
setHooksPreLoadSettings()

◆ setHooksPreLoadSettings()

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

Use to set custom behaviour before loading settings.

Parameters
hookPreloadCustom callback to use
See also
setHooksPostLoadSettings()

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