From b1020c599f146507ff06317a4496a395903c5376 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 1 Jan 2021 19:31:27 +0800 Subject: [PATCH] Improve load data behavior of SettingsStorage class Previously it only handle the case of failed lookup, now it discard invalid values when deserializing the database from disk. Also checks whether the data is convertible to the intended type. --- src/base/settingsstorage.cpp | 6 +++++- src/base/settingvalue.h | 10 +++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/base/settingsstorage.cpp b/src/base/settingsstorage.cpp index 54d5a70cf..790300ae7 100644 --- a/src/base/settingsstorage.cpp +++ b/src/base/settingsstorage.cpp @@ -295,7 +295,11 @@ QString TransactionalSettings::deserialize(const QString &name, QVariantHash &da // or that we don't touch directly in this code (eg disabled by ifdef). This ensures // that they will be copied over when save our settings to disk. for (const QString &key : asConst(settings->allKeys())) - data.insert(key, settings->value(key)); + { + const QVariant value = settings->value(key); + if (value.isValid()) + data[key] = value; + } return settings->fileName(); } diff --git a/src/base/settingvalue.h b/src/base/settingvalue.h index 46be9c1c1..02e701257 100644 --- a/src/base/settingvalue.h +++ b/src/base/settingvalue.h @@ -48,12 +48,16 @@ public: T get(const T &defaultValue = {}) const { - if constexpr (std::is_enum_v) { + if constexpr (std::is_enum_v) + { const auto value = SettingsStorage::instance()->loadValue(m_keyName, {}).toString(); return Utils::String::toEnum(value, defaultValue); } - else { - return SettingsStorage::instance()->loadValue(m_keyName, defaultValue).template value(); + else + { + const QVariant value = SettingsStorage::instance()->loadValue(m_keyName); + // check if retrieved value is convertible to T + return value.template canConvert() ? value.template value() : defaultValue; } }