|
| 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 |
| | Use to retrieve value from .ini file.
|
| |
| QString | getString (TOOLBOXQT_QTCOMPAT_STR_VIEW key, const QString &defaultValue=QString()) const |
| | Use to retrieve a string from .ini file.
|
| |
| void | setHooksPreLoadSettings (CbHook hookPreload) |
| | Use to set custom behaviour before loading settings.
|
| |
| void | setHooksPostLoadSettings (CbHook hookPostload) |
| | Use to set custom behaviour after loading settings.
|
| |
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:
4#include "toolboxqt/core/settingsini.h"
6#include <QVersionNumber>
8#define mPrefs (Preferences::instance())
10class Preferences final :
public QObject
13 APP_DISABLE_COPY_MOVE(Preferences)
16 static Preferences& instance();
19 void setup(
const QFileInfo &fileInfo);
25 bool preLoadSettings(
const QFileInfo &fileInfo);
26 bool postLoadSettings(
const QFileInfo &fileInfo);
32 static const QVersionNumber SUPPORT_VERSION;
Class used to manage INI configuration file.
Definition settingsini.h:15
- Source file:
1#include "preferences.h"
3#include "toolboxqt/core/corehelper.h"
5const QVersionNumber Preferences::SUPPORT_VERSION = QVersionNumber(1, 0, 0);
7Preferences& Preferences::instance()
9 static Preferences instance;
13Preferences::Preferences()
18void Preferences::setup(
const QFileInfo &fileInfo)
21 m_settings.setHooksPreLoadSettings(std::bind(&Preferences::preLoadSettings,
this, std::placeholders::_1));
22 m_settings.setHooksPostLoadSettings(std::bind(&Preferences::postLoadSettings,
this, std::placeholders::_1));
24 m_settings.loadSettings(fileInfo);
27bool Preferences::preLoadSettings(
const QFileInfo &fileInfo)
30 const QString filePath = fileInfo.absoluteFilePath();
31 if(!QFile::exists(filePath)){
32 const QString err = QString(
"Could not find configuration file: %1").arg(filePath);
40bool Preferences::postLoadSettings(APP_VAR_UNUSED
const QFileInfo &fileInfo)
42 static const QString KEY_VERSION_FILE =
"app/version_cfg_file";
45 const QString semverStr = m_settings.getValue(KEY_VERSION_FILE).toString();
46 const QVersionNumber semver = QVersionNumber::fromString(semverStr);
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());
57 m_settings.setValue(KEY_VERSION_FILE, SUPPORT_VERSION.toString());
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:
5#include "preferences.h"
10#define APP_CFG_FILE "configurations/configuration.ini"
15int main(
int argc,
char *argv[])
18 QApplication app(argc, argv);
21 Preferences::instance().setup(QFileInfo(APP_CFG_FILE));
24 MainWindow mainWindow;
Then we can use it anywhere with:
mPrefs.getValue("mySection/myKey");