diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-07-05 21:50:43 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-07-07 11:14:17 +0200 |
| commit | 3dfc872e356dd944d9cd03bccd8df5da8b1de472 (patch) | |
| tree | 076b9643cba6f30c940a57e4547b10cced9bc4c4 /sources/pyside6/PySide6 | |
| parent | 88075cc356706c2a9a1771a01e246ec92c014b0e (diff) | |
Fix 3 strange cases in QSettings.value() round trips of a list
- The "default value" parameter had an int 0 default value, which lead
to strange behavior ['0'] being returned for string lists with
missing keys.
- QStringList was not converted when forcing the type to be list
due to the QByteArray split mechanism.
- String values could not be coerced to a list
Fix the default value to have a default {}.
Add a helper function checking whether a custom type conversion
is actually needed or the default QVariant converter can handle it.
Pick-to: 6.5
Task-number: PYSIDE-2381
Change-Id: I91b22c05f851c2dc8c3792bd9f1446cfc8ceba51
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside6/PySide6')
| -rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index 8b445ba79..53cf50b22 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -99,6 +99,13 @@ static PyObject *convertToPrimitiveType(const QVariant &out, int metaTypeId) static PyObject *settingsTypeCoercion(const QVariant &out, PyTypeObject *typeObj) { if (typeObj == &PyList_Type) { + // Convert any string, etc, to a list of 1 element + if (auto *primitiveValue = convertToPrimitiveType(out, out.typeId())) { + PyObject *list = PyList_New(1); + PyList_SET_ITEM(list, 0, primitiveValue); + return list; + } + const QByteArray out_ba = out.toByteArray(); if (out_ba.isEmpty()) return PyList_New(0); @@ -132,6 +139,36 @@ static PyObject *settingsTypeCoercion(const QVariant &out, PyTypeObject *typeObj return nullptr; } +static bool isEquivalentSettingsType(PyTypeObject *typeObj, int metaTypeId) +{ + switch (metaTypeId) { + case QMetaType::QVariantList: + case QMetaType::QStringList: + return typeObj == &PyList_Type; + case QMetaType::QByteArray: + return typeObj == &PyBytes_Type; + case QMetaType::QString: + return typeObj == &PyUnicode_Type; + case QMetaType::Short: + case QMetaType::Long: + case QMetaType::LongLong: + case QMetaType::UShort: + case QMetaType::ULong: + case QMetaType::ULongLong: + case QMetaType::Int: + case QMetaType::UInt: + return typeObj == &PyLong_Type; + case QMetaType::Double: + case QMetaType::Float: + case QMetaType::Float16: + return typeObj == &PyFloat_Type; + case QMetaType::Bool: + return typeObj == &PyBool_Type; + default: + break; + } + return false; +} // @snippet settings-value-helpers // @snippet qsettings-value @@ -154,7 +191,8 @@ if ((kwds && PyDict_Size(kwds) > 0) || numArgs > 1) { PyTypeObject *typeObj = reinterpret_cast<PyTypeObject*>(%PYARG_3); -if (typeObj && !Shiboken::ObjectType::checkType(typeObj)) { +if (typeObj && !Shiboken::ObjectType::checkType(typeObj) + && !isEquivalentSettingsType(typeObj, out.typeId())) { %PYARG_0 = settingsTypeCoercion(out, typeObj); } else { if (out.isValid()) { |
