QSettings: Add support for QMetaType::Float

When writing out a float value, the output string is encoded as QVariant
for no reason. Looks like an oversight when QMetaType::Float was added a
long time ago.

Fixes: QTBUG-21156
Change-Id: I7f5d31e15892d700c1b1e5e731b7733ce3a15730
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Christian Ehrlicher 2021-01-03 21:40:52 +01:00
parent 539553a572
commit c23e8cb582
5 changed files with 29 additions and 0 deletions

View File

@ -403,6 +403,7 @@ QString QSettingsPrivate::variantToString(const QVariant &v)
case QMetaType::Int: case QMetaType::Int:
case QMetaType::UInt: case QMetaType::UInt:
case QMetaType::Bool: case QMetaType::Bool:
case QMetaType::Float:
case QMetaType::Double: { case QMetaType::Double: {
result = v.toString(); result = v.toString();
if (result.contains(QChar::Null)) if (result.contains(QChar::Null))

View File

@ -26,6 +26,7 @@ set(qsettings_resource_files
"resourcefile5.ini" "resourcefile5.ini"
"resourcefile6.plist" "resourcefile6.plist"
"withcomments.ini" "withcomments.ini"
"float.ini"
) )
qt_internal_add_resource(tst_qsettings "qsettings" qt_internal_add_resource(tst_qsettings "qsettings"

View File

@ -0,0 +1,3 @@
[test]
float=0.5
float_qvariant=@Variant(\0\0\0\x87?\0\0\0)

View File

@ -8,5 +8,6 @@
<file>resourcefile6.plist</file> <file>resourcefile6.plist</file>
<file>bom.ini</file> <file>bom.ini</file>
<file>withcomments.ini</file> <file>withcomments.ini</file>
<file>float.ini</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -189,6 +189,7 @@ private slots:
void embeddedZeroByte_data(); void embeddedZeroByte_data();
void embeddedZeroByte(); void embeddedZeroByte();
void spaceAfterComment(); void spaceAfterComment();
void floatAsQVariant();
void testXdg(); void testXdg();
private: private:
@ -765,6 +766,20 @@ void tst_QSettings::spaceAfterComment()
settings.endGroup(); settings.endGroup();
} }
// test if a qvariant-encoded float can be read
void tst_QSettings::floatAsQVariant()
{
QVERIFY(QFile::exists(":/float.ini"));
QSettings s(":/float.ini", QSettings::IniFormat);
s.beginGroup("test");
QCOMPARE(s.value("float").toDouble(), 0.5);
QCOMPARE(s.value("float_qvariant").toDouble(), 0.5);
QCOMPARE(s.value("float").toFloat(), 0.5);
QCOMPARE(s.value("float_qvariant").toFloat(), 0.5);
}
void tst_QSettings::testErrorHandling_data() void tst_QSettings::testErrorHandling_data()
{ {
QTest::addColumn<int>("filePerms"); // -1 means file should not exist QTest::addColumn<int>("filePerms"); // -1 means file should not exist
@ -1090,6 +1105,14 @@ void tst_QSettings::setValue()
settings.setValue("key 2", QString("false")); settings.setValue("key 2", QString("false"));
QCOMPARE(settings.value("key 2", true).toBool(), false); QCOMPARE(settings.value("key 2", true).toBool(), false);
settings.setValue("key 2", double(1234.56));
QCOMPARE(settings.value("key 2").toDouble(), double(1234.56));
QCOMPARE(settings.value("key 2").toString().left(7), QString::number(double(1234.56)));
settings.setValue("key 2", float(1234.56));
QCOMPARE(settings.value("key 2").toFloat(), float(1234.56));
QCOMPARE(settings.value("key 2").toString().left(7), QString::number(float(1234.56)));
// The following block should not compile. // The following block should not compile.
/* /*
settings.setValue("key 2", "true"); settings.setValue("key 2", "true");