Skip to content

Commit f3bcbfd

Browse files
author
Ulf Hermann
committed
QtQml: Re-allow assigning of raw numbers to enum property aliases
This used to work and we cannot just take it away. Amends commit 3ea55bf. Pick-to: 6.7 6.6 6.5 6.2 Fixes: QTBUG-121710 Change-Id: I7f856140286bba9d49b7ed1abfdf398a65fb1962 Reviewed-by: Fabian Kosmale <[email protected]>
1 parent 04c1588 commit f3bcbfd

File tree

7 files changed

+38
-4
lines changed

7 files changed

+38
-4
lines changed

src/qml/common/qv4compileddata_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ struct Binding
707707
}
708708
bool evaluatesToString() const { return type() == Type_String || isTranslationBinding(); }
709709

710+
bool isNumberBinding() const { return type() == Type_Number; }
711+
710712
bool valueAsBoolean() const
711713
{
712714
if (type() == Type_Boolean)

src/qml/qml/qqmlobjectcreator.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,11 @@ void QQmlObjectCreator::setPropertyValue(const QQmlPropertyData *property, const
315315
QMetaType propertyType = property->propType();
316316

317317
if (property->isEnum()) {
318-
if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum)) {
319-
propertyType = QMetaType::fromType<int>();
318+
if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum) ||
319+
// TODO: For historical reasons you can assign any number to an enum property alias
320+
// This can be fixed with an opt-out mechanism, for example a pragma.
321+
(property->isAlias() && binding->isNumberBinding())) {
322+
propertyType = property->propType().underlyingType();
320323
} else {
321324
// ### This should be resolved earlier at compile time and the binding value should be changed accordingly.
322325
QVariant value = compilationUnit->bindingValueAsString(binding);

src/qml/qml/qqmlpropertycachecreator_p.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,10 +905,10 @@ inline QQmlError QQmlPropertyCacheAliasCreator<ObjectContainer>::propertyDataFor
905905
resettable = property->isResettable();
906906
bindable = property->isBindable();
907907

908-
// Copy type flags
909-
propertyFlags->copyPropertyTypeFlags(property->flags());
910908
if (property->isVarProperty())
911909
propertyFlags->setType(QQmlPropertyData::Flags::QVariantType);
910+
else
911+
propertyFlags->copyPropertyTypeFlags(property->flags());
912912
};
913913

914914
// for deep aliases, valueTypeIndex is always set

src/qml/qml/qqmlpropertyvalidator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ QQmlError QQmlPropertyValidator::validateLiteralBinding(
374374
if (binding->hasFlag(QV4::CompiledData::Binding::IsResolvedEnum))
375375
return noError;
376376

377+
// TODO: For historical reasons you can assign any number to an enum property alias
378+
// This can be fixed with an opt-out mechanism, for example a pragma.
379+
if (property->isAlias() && binding->isNumberBinding())
380+
return noError;
381+
377382
QString value = compilationUnit->bindingValueAsString(binding);
378383
QMetaProperty p = propertyCache->firstCppMetaObject()->property(property->coreIndex());
379384
bool ok;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import QtQml
2+
3+
QtObject {
4+
property alias strokeStyle: path.restoreMode
5+
property Binding p: Binding { id: path }
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import QtQml
2+
3+
AliasHolder {
4+
strokeStyle: 1
5+
}

tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ private slots:
442442

443443
void ambiguousComponents();
444444

445+
void writeNumberToEnumAlias();
446+
445447
private:
446448
QQmlEngine engine;
447449
QStringList defaultImportPathList;
@@ -8501,6 +8503,17 @@ void tst_qqmllanguage::ambiguousComponents()
85018503
QVERIFY(isInstanceOf);
85028504
}
85038505

8506+
void tst_qqmllanguage::writeNumberToEnumAlias()
8507+
{
8508+
QQmlEngine engine;
8509+
QQmlComponent c(&engine, testFileUrl("aliasWriter.qml"));
8510+
QVERIFY2(c.isReady(), qPrintable(c.errorString()));
8511+
QScopedPointer<QObject> o(c.create());
8512+
QVERIFY(!o.isNull());
8513+
8514+
QCOMPARE(o->property("strokeStyle").toInt(), 1);
8515+
}
8516+
85048517
QTEST_MAIN(tst_qqmllanguage)
85058518

85068519
#include "tst_qqmllanguage.moc"

0 commit comments

Comments
 (0)