From a2a8cea2835ef24104fe784b6ce0f508cc5637c0 Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Wed, 24 Feb 2010 15:06:56 +1000 Subject: Automatically connect to a notify signal if the requested signal is in the "onFooChanged" form, even if the notify signal is not called "fooChanged". Task-number: QT-2783 --- src/declarative/qml/qdeclarativecompiler.cpp | 10 ++++++++++ .../qdeclarativelanguage/data/autoNotifyConnection.qml | 6 ++++++ tests/auto/declarative/qdeclarativelanguage/testtypes.h | 6 ++++++ .../qdeclarativelanguage/tst_qdeclarativelanguage.cpp | 17 +++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 tests/auto/declarative/qdeclarativelanguage/data/autoNotifyConnection.qml diff --git a/src/declarative/qml/qdeclarativecompiler.cpp b/src/declarative/qml/qdeclarativecompiler.cpp index 0593a5d..b6ebd60 100644 --- a/src/declarative/qml/qdeclarativecompiler.cpp +++ b/src/declarative/qml/qdeclarativecompiler.cpp @@ -1326,6 +1326,16 @@ int QDeclarativeCompiler::findSignalByName(const QMetaObject *mo, const QByteArr if (methodName == name) return ii; } + + // If no signal is found, but the signal is of the form "onBlahChanged", + // return the notify signal for the property "Blah" + if (name.endsWith("Changed")) { + QByteArray propName = name.mid(0, name.length() - 7); + int propIdx = mo->indexOfProperty(propName.constData()); + if (propIdx >= 0) + return mo->property(propIdx).notifySignalIndex(); + } + return -1; } diff --git a/tests/auto/declarative/qdeclarativelanguage/data/autoNotifyConnection.qml b/tests/auto/declarative/qdeclarativelanguage/data/autoNotifyConnection.qml new file mode 100644 index 0000000..640fb54 --- /dev/null +++ b/tests/auto/declarative/qdeclarativelanguage/data/autoNotifyConnection.qml @@ -0,0 +1,6 @@ +import Test 1.0 +MyQmlObject { + property bool receivedNotify : false + onPropertyWithNotifyChanged: { receivedNotify = true; } +} + diff --git a/tests/auto/declarative/qdeclarativelanguage/testtypes.h b/tests/auto/declarative/qdeclarativelanguage/testtypes.h index 21f3e70..fa62eb4 100644 --- a/tests/auto/declarative/qdeclarativelanguage/testtypes.h +++ b/tests/auto/declarative/qdeclarativelanguage/testtypes.h @@ -102,6 +102,7 @@ class MyQmlObject : public QObject, public MyInterface, public QDeclarativeParse Q_PROPERTY(int onLiteralSignal READ onLiteralSignal WRITE setOnLiteralSignal); Q_PROPERTY(MyCustomVariantType customType READ customType WRITE setCustomType); Q_PROPERTY(MyQmlObject *qmlobjectProperty READ qmlobject WRITE setQmlobject) + Q_PROPERTY(int propertyWithNotify READ propertyWithNotify WRITE setPropertyWithNotify NOTIFY oddlyNamedNotifySignal) Q_INTERFACES(MyInterface QDeclarativeParserStatus) public: @@ -137,6 +138,9 @@ public: MyCustomVariantType customType() const { return m_custom; } void setCustomType(const MyCustomVariantType &v) { m_custom = v; } + + int propertyWithNotify() const { return m_propertyWithNotify; } + void setPropertyWithNotify(int i) { m_propertyWithNotify = i; emit oddlyNamedNotifySignal(); } public slots: void basicSlot() { qWarning("MyQmlObject::basicSlot"); } void basicSlotWithArgs(int v) { qWarning("MyQmlObject::basicSlotWithArgs(%d)", v); } @@ -144,6 +148,7 @@ public slots: signals: void basicSignal(); void basicParameterizedSignal(int parameter); + void oddlyNamedNotifySignal(); private: friend class tst_qmllanguage; @@ -151,6 +156,7 @@ private: MyInterface *m_interface; MyQmlObject *m_qmlobject; MyCustomVariantType m_custom; + int m_propertyWithNotify; }; QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES) QML_DECLARE_TYPE(MyQmlObject); diff --git a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp index cb59f47..b177636 100644 --- a/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp +++ b/tests/auto/declarative/qdeclarativelanguage/tst_qdeclarativelanguage.cpp @@ -88,6 +88,7 @@ private slots: void rootAsQmlComponent(); void inlineQmlComponents(); void idProperty(); + void autoNotifyConnection(); void assignSignal(); void dynamicProperties(); void dynamicPropertiesNested(); @@ -511,6 +512,22 @@ void tst_qmllanguage::idProperty() QCOMPARE(object->property("object"), QVariant::fromValue((QObject *)child)); } +// Tests automatic connection to notify signals if "onBlahChanged" syntax is used +// even if the notify signal for "blah" is not called "blahChanged" +void tst_qmllanguage::autoNotifyConnection() +{ + QDeclarativeComponent component(&engine, TEST_FILE("autoNotifyConnection.qml")); + VERIFY_ERRORS(0); + MyQmlObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + QMetaProperty prop = object->metaObject()->property(object->metaObject()->indexOfProperty("receivedNotify")); + QVERIFY(prop.isValid()); + + QCOMPARE(prop.read(object), QVariant::fromValue(false)); + object->setPropertyWithNotify(1); + QCOMPARE(prop.read(object), QVariant::fromValue(true)); +} + // Tests that signals can be assigned to void tst_qmllanguage::assignSignal() { -- cgit v0.12