diff --git a/src/dconfigfile.cpp b/src/dconfigfile.cpp index 896248a8..6a7da03d 100644 --- a/src/dconfigfile.cpp +++ b/src/dconfigfile.cpp @@ -1288,12 +1288,22 @@ class Q_DECL_HIDDEN DConfigFilePrivate : public DObjectPrivate { return cache->setValue(key, value, configMeta->serial(key), cache->uid(), appid); // convert copy to meta's type, it promises `setValue` don't change meta's type. + // canConvert isn't explicit, e.g: QString is also can convert to double. auto copy = value; if (!copy.convert(metaValue.metaType())) { qCWarning(cfLog) << "check type error, meta type is " << metaValue.metaType().name() << ", and now type is " << value.metaType().name(); return false; } + + // TODO it's a bug of qt, MetaType of 1.0 is qlonglong instead of double in json file. + static const QVector filterConvertType { + QMetaType{QMetaType::Double} + }; + // reset to origin value. + if (filterConvertType.contains(value.metaType())) { + copy = value; + } #else if (metaValue.type() == value.type()) return cache->setValue(key, value, configMeta->serial(key), cache->uid(), appid); @@ -1304,6 +1314,13 @@ class Q_DECL_HIDDEN DConfigFilePrivate : public DObjectPrivate { << ", and now type is " << value.type(); return false; } + + static const QVector filterConvertType { + QVariant::Double + }; + if (filterConvertType.contains(value.type())) { + copy = value; + } #endif return cache->setValue(key, copy, configMeta->serial(key), cache->uid(), appid); diff --git a/tests/data/dconf-example.meta.json b/tests/data/dconf-example.meta.json index abe786f0..4063fab8 100755 --- a/tests/data/dconf-example.meta.json +++ b/tests/data/dconf-example.meta.json @@ -40,6 +40,14 @@ "permissions": "readwrite", "visibility": "public" }, + "numberDouble": { + "value": 1.0, + "serial": 0, + "flags": ["global"], + "name": "double value type", + "permissions": "readwrite", + "visibility": "public" + }, "array": { "value": ["value1", "value2"], "serial": 0, diff --git a/tests/ut_dconfigfile.cpp b/tests/ut_dconfigfile.cpp index 8b301ce3..9efb5d24 100644 --- a/tests/ut_dconfigfile.cpp +++ b/tests/ut_dconfigfile.cpp @@ -124,6 +124,11 @@ TEST_F(ut_DConfigFile, setValueTypeCheck) { ASSERT_FALSE(config.setValue("number", "1ab", "test", userCache.get())); ASSERT_EQ(config.value("number", userCache.get()).type(), type); } + { + const auto type = config.value("numberDouble", userCache.get()).type(); + ASSERT_TRUE(config.setValue("numberDouble", 1.2, "test", userCache.get())); + ASSERT_EQ(config.value("numberDouble", userCache.get()), 1.2); + } { const auto type = config.value("array", userCache.get()).type(); const QStringList array{"value1", "value2"};