aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2024-01-31 17:49:42 +0100
committerUlf Hermann <ulf.hermann@qt.io>2024-02-01 16:32:51 +0100
commitf3bcbfd6a50fb7e74f4ff6714d3b3066fa74e253 (patch)
tree675580377465f8988e3903aad22f248bd784de46
parent04c1588965b9a4b12ebf04b23e5336fe88bb4f56 (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.7 6.6 6.5 6.2 Fixes: QTBUG-121710 Change-Id: I7f856140286bba9d49b7ed1abfdf398a65fb1962 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qml/common/qv4compileddata_p.h2
-rw-r--r--src/qml/qml/qqmlobjectcreator.cpp7
-rw-r--r--src/qml/qml/qqmlpropertycachecreator_p.h4
-rw-r--r--src/qml/qml/qqmlpropertyvalidator.cpp5
-rw-r--r--tests/auto/qml/qqmllanguage/data/AliasHolder.qml6
-rw-r--r--tests/auto/qml/qqmllanguage/data/aliasWriter.qml5
-rw-r--r--tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp13
7 files changed, 38 insertions, 4 deletions
diff --git a/src/qml/common/qv4compileddata_p.h b/src/qml/common/qv4compileddata_p.h
index 55e73bba7d..f58a176009 100644
--- a/src/qml/common/qv4compileddata_p.h
+++ b/src/qml/common/qv4compileddata_p.h
@@ -707,6 +707,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 ec7d0b42ce..3d4d3596cc 100644
--- a/src/qml/qml/qqmlobjectcreator.cpp
+++ b/src/qml/qml/qqmlobjectcreator.cpp
@@ -315,8 +315,11 @@ void QQmlObjectCreator::setPropertyValue(const QQmlPropertyData *property, const
QMetaType propertyType = property->propType();
if (property->isEnum()) {
- if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum)) {
- propertyType = QMetaType::fromType<int>();
+ 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 = property->propType().underlyingType();
} else {
// ### This should be resolved earlier at compile time and the binding value should be changed accordingly.
QVariant value = compilationUnit->bindingValueAsString(binding);
diff --git a/src/qml/qml/qqmlpropertycachecreator_p.h b/src/qml/qml/qqmlpropertycachecreator_p.h
index 414e7aa728..fe0cdb5cd4 100644
--- a/src/qml/qml/qqmlpropertycachecreator_p.h
+++ b/src/qml/qml/qqmlpropertycachecreator_p.h
@@ -905,10 +905,10 @@ inline QQmlError QQmlPropertyCacheAliasCreator<ObjectContainer>::propertyDataFor
resettable = property->isResettable();
bindable = property->isBindable();
- // Copy type flags
- propertyFlags->copyPropertyTypeFlags(property->flags());
if (property->isVarProperty())
propertyFlags->setType(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 82fd072698..a49393e425 100644
--- a/src/qml/qml/qqmlpropertyvalidator.cpp
+++ b/src/qml/qml/qqmlpropertyvalidator.cpp
@@ -374,6 +374,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 9882f92535..8ad8433daa 100644
--- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
+++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
@@ -442,6 +442,8 @@ private slots:
void ambiguousComponents();
+ void writeNumberToEnumAlias();
+
private:
QQmlEngine engine;
QStringList defaultImportPathList;
@@ -8501,6 +8503,17 @@ void tst_qqmllanguage::ambiguousComponents()
QVERIFY(isInstanceOf);
}
+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"