setting增加线程同步功能

This commit is contained in:
hewenfei 2022-12-12 14:18:13 +08:00
parent 0e9ace9a5c
commit c806406eab
2 changed files with 17 additions and 1 deletions

View File

@ -52,7 +52,7 @@ void GlobalSetting::initStyleSetting()
connect(settings, &QGSettings::changed, this, [this, settings] (const QString &key) { connect(settings, &QGSettings::changed, this, [this, settings] (const QString &key) {
if (key == UKUI_STYLE_NAME_KEY) { if (key == UKUI_STYLE_NAME_KEY) {
m_cache.insert(StyleName, settings->get(key)); updateData(StyleName, settings->get(key));
Q_EMIT styleChanged(StyleName); Q_EMIT styleChanged(StyleName);
} }
}); });
@ -61,9 +61,16 @@ void GlobalSetting::initStyleSetting()
QVariant GlobalSetting::get(const GlobalSetting::Key& key) QVariant GlobalSetting::get(const GlobalSetting::Key& key)
{ {
QMutexLocker locker(&mutex);
return m_cache.value(key); return m_cache.value(key);
} }
void GlobalSetting::updateData(const GlobalSetting::Key &key, const QVariant &value)
{
QMutexLocker locker(&mutex);
m_cache.insert(key, value);
}
MenuSetting *MenuSetting::instance() MenuSetting *MenuSetting::instance()
{ {
static MenuSetting setting(nullptr); static MenuSetting setting(nullptr);
@ -98,11 +105,13 @@ void MenuSetting::initSettings()
QVariant MenuSetting::get(const QString &key) QVariant MenuSetting::get(const QString &key)
{ {
QMutexLocker locker(&mutex);
return m_cache.value(key, {}); return m_cache.value(key, {});
} }
bool MenuSetting::set(const QString &key, const QVariant &value) bool MenuSetting::set(const QString &key, const QVariant &value)
{ {
QMutexLocker locker(&mutex);
if (m_gSettings) { if (m_gSettings) {
m_gSettings->set(key, value); m_gSettings->set(key, value);
// m_cache.insert(key,value); // m_cache.insert(key,value);
@ -115,7 +124,9 @@ bool MenuSetting::set(const QString &key, const QVariant &value)
void MenuSetting::updateValue(const QString &key) void MenuSetting::updateValue(const QString &key)
{ {
mutex.lock();
m_cache.insert(key, m_gSettings->get(key)); m_cache.insert(key, m_gSettings->get(key));
mutex.unlock();
Q_EMIT changed(key); Q_EMIT changed(key);
} }

View File

@ -22,6 +22,8 @@
#include <QObject> #include <QObject>
#include <QGSettings> #include <QGSettings>
#include <QVariant> #include <QVariant>
#include <QMutex>
#include <QMutexLocker>
#define UKUI_STYLE_VALUE_DARK "ukui-dark" #define UKUI_STYLE_VALUE_DARK "ukui-dark"
#define UKUI_STYLE_VALUE_LIGHT "ukui-light" #define UKUI_STYLE_VALUE_LIGHT "ukui-light"
@ -55,8 +57,10 @@ Q_SIGNALS:
private: private:
explicit GlobalSetting(QObject *parent = nullptr); explicit GlobalSetting(QObject *parent = nullptr);
void initStyleSetting(); void initStyleSetting();
void updateData(const GlobalSetting::Key& key, const QVariant &value);
private: private:
QMutex mutex;
QMap<GlobalSetting::Key, QVariant> m_cache; QMap<GlobalSetting::Key, QVariant> m_cache;
}; };
@ -83,6 +87,7 @@ private:
void initSettings(); void initSettings();
private: private:
QMutex mutex;
QMap<QString, QVariant> m_cache; QMap<QString, QVariant> m_cache;
QGSettings *m_gSettings{nullptr}; QGSettings *m_gSettings{nullptr};
}; };