From f876562de8eb978cea39fe72e76c49ae51ff2f97 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 23 Oct 2014 12:05:44 +0200 Subject: Fix syntax error when trying to declare read-only object properties The grammar did not allow for the declaration of readonly property QtObject foo: QtObject { ... } and it required a workaround through an alias: readonly property alias foo: _foo property QtObject _foo: QtObject { ... } This was merely a glitch in the grammar, I see no reason not to support this. The semantics are like a const pointer in C++, the property itself is read-only but the object pointed to has per-property defined read/write semantics. Task-number: QTBUG-41971 Change-Id: I99e2e7ed58731e387a38e46ec39922d280a21ceb Reviewed-by: Michael Brasser Reviewed-by: Lars Knoll --- .../qml/qqmllanguage/data/readonlyObjectProperty.qml | 7 +++++++ tests/auto/qml/qqmllanguage/qqmllanguage.pro | 3 +++ tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 tests/auto/qml/qqmllanguage/data/readonlyObjectProperty.qml (limited to 'tests') diff --git a/tests/auto/qml/qqmllanguage/data/readonlyObjectProperty.qml b/tests/auto/qml/qqmllanguage/data/readonlyObjectProperty.qml new file mode 100644 index 0000000000..3fc332f565 --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/readonlyObjectProperty.qml @@ -0,0 +1,7 @@ +import QtQml 2.0 + +QtObject { + readonly property QtObject subObject: QtObject { + property int readWrite: 42 + } +} diff --git a/tests/auto/qml/qqmllanguage/qqmllanguage.pro b/tests/auto/qml/qqmllanguage/qqmllanguage.pro index 3b0518cfdf..f0c8bb6c1b 100644 --- a/tests/auto/qml/qqmllanguage/qqmllanguage.pro +++ b/tests/auto/qml/qqmllanguage/qqmllanguage.pro @@ -16,3 +16,6 @@ QT += core-private gui-private qml-private network testlib include (../../shared/util.pri) DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 + +OTHER_FILES += \ + data/readonlyObjectProperty.qml diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index ec3e38dd14..12d2f53596 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -151,6 +151,7 @@ private slots: void nestedComponentRoots(); void registrationOrder(); void readonly(); + void readonlyObjectProperties(); void receivers(); void registeredCompositeType(); void implicitImportsLast(); @@ -3274,6 +3275,24 @@ void tst_qqmllanguage::readonly() delete o; } +void tst_qqmllanguage::readonlyObjectProperties() +{ + QQmlComponent component(&engine, testFileUrl("readonlyObjectProperty.qml")); + + QScopedPointer o(component.create()); + QVERIFY(!o.isNull()); + + QQmlProperty prop(o.data(), QStringLiteral("subObject"), &engine); + QVERIFY(!prop.isWritable()); + QVERIFY(!prop.write(QVariant::fromValue(o.data()))); + + QObject *subObject = qvariant_cast(prop.read()); + QVERIFY(subObject); + QCOMPARE(subObject->property("readWrite").toInt(), int(42)); + subObject->setProperty("readWrite", QVariant::fromValue(int(100))); + QCOMPARE(subObject->property("readWrite").toInt(), int(100)); +} + void tst_qqmllanguage::receivers() { QQmlComponent component(&engine, testFileUrl("receivers.qml")); -- cgit v1.2.3