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.
This commit is contained in:
Vasily
2019-12-03 08:26:10 +01:00
committed by username223
parent 4eb8a35d6b
commit 6cf3b582f6
2 changed files with 14 additions and 23 deletions
+13 -4
View File
@@ -75,11 +75,20 @@ void saveQtSetting( const SettingCategory category,
getQSettings().endGroup();
}
[[nodiscard]] bool isValidQVariant( const QVariant v,
const QMetaType::Type type )
template <typename Value>[[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, std::string>::value )
{
// Special case for std::string because Qt refuses to recognize it
savedSettingIsValid = savedSettingIsValid && v.canConvert<QString>();
}
else
{
savedSettingIsValid = savedSettingIsValid && v.canConvert<Value>();
}
if ( savedSettingIsValid )
{
return true;
+1 -19
View File
@@ -26,25 +26,7 @@ public:
const auto v = getQtSetting( SettingValue::category(),
SettingValue::qtInfo().settingName );
QMetaType::Type type;
if constexpr ( std::is_same<Value, bool>::value )
{
type = QMetaType::Bool;
}
else if constexpr ( std::is_same<Value, double>::value )
{
type = QMetaType::Double;
}
else if constexpr ( std::is_same<Value, std::string>::value )
{
type = QMetaType::QString;
}
else if constexpr ( std::is_same<Value, int>::value )
{
type = QMetaType::Int;
}
if ( isValidQVariant( v, type ) )
if ( isValidQVariant<Value>( v ) )
{
if constexpr ( std::is_same<Value, bool>::value )
{