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:
parent
539553a572
commit
c23e8cb582
@ -403,6 +403,7 @@ QString QSettingsPrivate::variantToString(const QVariant &v)
|
||||
case QMetaType::Int:
|
||||
case QMetaType::UInt:
|
||||
case QMetaType::Bool:
|
||||
case QMetaType::Float:
|
||||
case QMetaType::Double: {
|
||||
result = v.toString();
|
||||
if (result.contains(QChar::Null))
|
||||
|
@ -26,6 +26,7 @@ set(qsettings_resource_files
|
||||
"resourcefile5.ini"
|
||||
"resourcefile6.plist"
|
||||
"withcomments.ini"
|
||||
"float.ini"
|
||||
)
|
||||
|
||||
qt_internal_add_resource(tst_qsettings "qsettings"
|
||||
|
3
tests/auto/corelib/io/qsettings/float.ini
Normal file
3
tests/auto/corelib/io/qsettings/float.ini
Normal file
@ -0,0 +1,3 @@
|
||||
[test]
|
||||
float=0.5
|
||||
float_qvariant=@Variant(\0\0\0\x87?\0\0\0)
|
@ -8,5 +8,6 @@
|
||||
<file>resourcefile6.plist</file>
|
||||
<file>bom.ini</file>
|
||||
<file>withcomments.ini</file>
|
||||
<file>float.ini</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
@ -189,6 +189,7 @@ private slots:
|
||||
void embeddedZeroByte_data();
|
||||
void embeddedZeroByte();
|
||||
void spaceAfterComment();
|
||||
void floatAsQVariant();
|
||||
|
||||
void testXdg();
|
||||
private:
|
||||
@ -765,6 +766,20 @@ void tst_QSettings::spaceAfterComment()
|
||||
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()
|
||||
{
|
||||
QTest::addColumn<int>("filePerms"); // -1 means file should not exist
|
||||
@ -1090,6 +1105,14 @@ void tst_QSettings::setValue()
|
||||
settings.setValue("key 2", QString("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.
|
||||
/*
|
||||
settings.setValue("key 2", "true");
|
||||
|
Loading…
Reference in New Issue
Block a user