fix subsribe to settings changes

This commit is contained in:
Andrei Yankovich 2021-11-20 13:02:57 +03:00
parent 6d5f602b7b
commit bd6dd8ecca
2 changed files with 48 additions and 12 deletions

View File

@ -7,26 +7,46 @@
#include "isettings.h"
#include "settingslistner.h"
#include "params.h"
namespace QuasarAppUtils {
SettingsListner::SettingsListner() {
auto settings = ISettings::instance();
if (settings) {
auto listner = [this](QString key, QVariant val){
this->handleSettingsChanged(key, val);
};
_listnerConnection = QObject::connect(settings,
&ISettings::valueChanged,
listner);
}
}
SettingsListner::~SettingsListner() {
QObject::disconnect(_listnerConnection);
}
bool SettingsListner::handleSettings() {
auto settings = ISettings::instance();
if (!settings) {
Params::log("Settings object is not initialised!"
"please invoke ISettings::init before subscribe",
Error);
return false;
}
auto listner = [this](QString key, QVariant val){
this->handleSettingsChanged(key, val);
};
QObject * thiz = dynamic_cast<QObject*>(this);
if (!thiz) {
Params::log("Object that use the SettingsListner should be QObject."
"The handleSettingsChanged method will not invoked when settings has changed",
Error);
return false;
}
_listnerConnection = thiz->connect(settings,
&ISettings::valueChanged,
thiz, listner,
Qt::QueuedConnection);
return true;
}
}

View File

@ -24,6 +24,12 @@ namespace QuasarAppUtils {
*
* @code{cpp}
* class MyClass : protected QuasarAppUtils::SettingsListner {
*
* public:
* MyClass(){
* handleSettings();
* }
*
* protected:
* void handleSettingsChanged(const QString& key, const QVariant& value) override {
*
@ -34,6 +40,9 @@ namespace QuasarAppUtils {
*
* };
* @endcode
*
* @warning For correctly working this handler you should be create a class that inherit of the QObject class else
* the SettingsListner::handleSettingsChanged will nit invoked when settings has changed.
* @see ISettings
*/
class QUASARAPPSHARED_EXPORT SettingsListner
@ -44,6 +53,13 @@ public:
protected:
/**
* @brief handleSettings This method subscribe this class to listning settings changes.
* @return true if subscribed successful else false.
* @see SettingsListner::handleSettingsChanged
*/
bool handleSettings();
/**
* @brief handleSettingsChanged This method will be invoked when settings of application has bean changed.
* @param key This is key of a changed settings.