From 6cf3b582f63574bc4d7bd4f4299c4b310c4630de Mon Sep 17 00:00:00 2001 From: Vasily Date: Sun, 1 Dec 2019 00:11:07 +0100 Subject: [PATCH] Fix QVariant not recognizing convertable types The QMetaType did not correctly recognize that bools and doubles were convertable from QVariant. The new method is more robust. --- src/settings/settings_internal.h | 17 +++++++++++++---- src/settings/specific_setting_value.h | 20 +------------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/settings/settings_internal.h b/src/settings/settings_internal.h index ddfc8b9..c8b3482 100644 --- a/src/settings/settings_internal.h +++ b/src/settings/settings_internal.h @@ -75,11 +75,20 @@ void saveQtSetting( const SettingCategory category, getQSettings().endGroup(); } -[[nodiscard]] bool isValidQVariant( const QVariant v, - const QMetaType::Type type ) +template [[nodiscard]] bool isValidQVariant( const QVariant v ) { - const auto savedSettingIsValid - = v.isValid() && !v.isNull() && ( v.userType() == type ); + auto savedSettingIsValid = v.isValid() && !v.isNull(); + + if constexpr ( std::is_same::value ) + { + // Special case for std::string because Qt refuses to recognize it + savedSettingIsValid = savedSettingIsValid && v.canConvert(); + } + else + { + savedSettingIsValid = savedSettingIsValid && v.canConvert(); + } + if ( savedSettingIsValid ) { return true; diff --git a/src/settings/specific_setting_value.h b/src/settings/specific_setting_value.h index 6981afb..119ead6 100644 --- a/src/settings/specific_setting_value.h +++ b/src/settings/specific_setting_value.h @@ -26,25 +26,7 @@ public: const auto v = getQtSetting( SettingValue::category(), SettingValue::qtInfo().settingName ); - QMetaType::Type type; - if constexpr ( std::is_same::value ) - { - type = QMetaType::Bool; - } - else if constexpr ( std::is_same::value ) - { - type = QMetaType::Double; - } - else if constexpr ( std::is_same::value ) - { - type = QMetaType::QString; - } - else if constexpr ( std::is_same::value ) - { - type = QMetaType::Int; - } - - if ( isValidQVariant( v, type ) ) + if ( isValidQVariant( v ) ) { if constexpr ( std::is_same::value ) {