diff options
| author | Ulf Hermann <ulf.hermann@qt.io> | 2024-01-31 17:49:42 +0100 |
|---|---|---|
| committer | Ulf Hermann <ulf.hermann@qt.io> | 2024-02-05 14:32:26 +0100 |
| commit | 444810849bcafe5d52927a2ae2f972d087d36193 (patch) | |
| tree | 85844ca1a43540b21426e888719e3e880c439dba | |
| parent | 6872342167acf2a20cb15ddf65d04b4e998ca286 (diff) | |
QtQml: Re-allow assigning of raw numbers to enum property aliases
This used to work and we cannot just take it away.
Amends commit 3ea55bf398412d373daab9c92b1498f45de70e96.
Pick-to: 6.2
Fixes: QTBUG-121710
Change-Id: I7f856140286bba9d49b7ed1abfdf398a65fb1962
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit f3bcbfd6a50fb7e74f4ff6714d3b3066fa74e253)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit ec692b22e7b45bef016280a15c8205969e808e12)
(cherry picked from commit 3a8028b393f6b5345f5653d81f49efa31021fae1)
| -rw-r--r-- | src/qml/common/qv4compileddata_p.h | 2 | ||||
| -rw-r--r-- | src/qml/qml/qqmlobjectcreator.cpp | 5 | ||||
| -rw-r--r-- | src/qml/qml/qqmlpropertycachecreator_p.h | 4 | ||||
| -rw-r--r-- | src/qml/qml/qqmlpropertyvalidator.cpp | 5 | ||||
| -rw-r--r-- | tests/auto/qml/qqmllanguage/data/AliasHolder.qml | 6 | ||||
| -rw-r--r-- | tests/auto/qml/qqmllanguage/data/aliasWriter.qml | 5 | ||||
| -rw-r--r-- | tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 13 |
7 files changed, 37 insertions, 3 deletions
diff --git a/src/qml/common/qv4compileddata_p.h b/src/qml/common/qv4compileddata_p.h index 47b64a6213..8dfb5f7fd6 100644 --- a/src/qml/common/qv4compileddata_p.h +++ b/src/qml/common/qv4compileddata_p.h @@ -670,6 +670,8 @@ struct Binding } bool evaluatesToString() const { return type() == Type_String || isTranslationBinding(); } + bool isNumberBinding() const { return type() == Type_Number; } + bool valueAsBoolean() const { if (type() == Type_Boolean) diff --git a/src/qml/qml/qqmlobjectcreator.cpp b/src/qml/qml/qqmlobjectcreator.cpp index 4b60dbe655..6b8d57807f 100644 --- a/src/qml/qml/qqmlobjectcreator.cpp +++ b/src/qml/qml/qqmlobjectcreator.cpp @@ -313,7 +313,10 @@ void QQmlObjectCreator::setPropertyValue(const QQmlPropertyData *property, const QMetaType propertyType = property->propType(); if (property->isEnum()) { - if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum)) { + if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum) || + // TODO: For historical reasons you can assign any number to an enum property alias + // This can be fixed with an opt-out mechanism, for example a pragma. + (property->isAlias() && binding->isNumberBinding())) { propertyType = QMetaType::fromType<int>(); } else { // ### This should be resolved earlier at compile time and the binding value should be changed accordingly. diff --git a/src/qml/qml/qqmlpropertycachecreator_p.h b/src/qml/qml/qqmlpropertycachecreator_p.h index 71f1cd7f0b..162e8d01a6 100644 --- a/src/qml/qml/qqmlpropertycachecreator_p.h +++ b/src/qml/qml/qqmlpropertycachecreator_p.h @@ -864,10 +864,10 @@ inline QQmlError QQmlPropertyCacheAliasCreator<ObjectContainer>::propertyDataFor resettable = property->isResettable(); bindable = property->isBindable(); - // Copy type flags - propertyFlags->copyPropertyTypeFlags(property->flags()); if (property->isVarProperty()) propertyFlags->type = QQmlPropertyData::Flags::QVariantType; + else + propertyFlags->copyPropertyTypeFlags(property->flags()); }; // for deep aliases, valueTypeIndex is always set diff --git a/src/qml/qml/qqmlpropertyvalidator.cpp b/src/qml/qml/qqmlpropertyvalidator.cpp index ead8f78f33..ad8cb5a617 100644 --- a/src/qml/qml/qqmlpropertyvalidator.cpp +++ b/src/qml/qml/qqmlpropertyvalidator.cpp @@ -364,6 +364,11 @@ QQmlError QQmlPropertyValidator::validateLiteralBinding( if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum)) return noError; + // TODO: For historical reasons you can assign any number to an enum property alias + // This can be fixed with an opt-out mechanism, for example a pragma. + if (property->isAlias() && binding->isNumberBinding()) + return noError; + QString value = compilationUnit->bindingValueAsString(binding); QMetaProperty p = propertyCache->firstCppMetaObject()->property(property->coreIndex()); bool ok; diff --git a/tests/auto/qml/qqmllanguage/data/AliasHolder.qml b/tests/auto/qml/qqmllanguage/data/AliasHolder.qml new file mode 100644 index 0000000000..42aed6ed26 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/AliasHolder.qml @@ -0,0 +1,6 @@ +import QtQml + +QtObject { + property alias strokeStyle: path.restoreMode + property Binding p: Binding { id: path } +} diff --git a/tests/auto/qml/qqmllanguage/data/aliasWriter.qml b/tests/auto/qml/qqmllanguage/data/aliasWriter.qml new file mode 100644 index 0000000000..4001c2af34 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/aliasWriter.qml @@ -0,0 +1,5 @@ +import QtQml + +AliasHolder { + strokeStyle: 1 +} diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index 1a19d19b3b..5b0a4346a5 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -424,6 +424,8 @@ private slots: void objectInQmlListAndGc(); void deepAliasOnICOrReadonly(); + void writeNumberToEnumAlias(); + private: QQmlEngine engine; QStringList defaultImportPathList; @@ -8143,6 +8145,17 @@ void tst_qqmllanguage::deepAliasOnICOrReadonly() "Invalid property assignment: \"readonlyRectX\" is a read-only property"))); } +void tst_qqmllanguage::writeNumberToEnumAlias() +{ + QQmlEngine engine; + QQmlComponent c(&engine, testFileUrl("aliasWriter.qml")); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer<QObject> o(c.create()); + QVERIFY(!o.isNull()); + + QCOMPARE(o->property("strokeStyle").toInt(), 1); +} + QTEST_MAIN(tst_qqmllanguage) #include "tst_qqmllanguage.moc" |
