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:
@@ -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;
|
||||
|
||||
@@ -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 )
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user