From ef785f047adf6f559c282d67e627b78b75a7b9ef Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Wed, 3 Mar 2010 17:53:05 +1000 Subject: QDeclarativeProperty::read() returns a QObjectStar when accessing a QObject property QT-2793 --- src/declarative/qml/qdeclarativeproperty.cpp | 11 +++++-- .../qdeclarativeproperty/data/TestType.qml | 6 ++++ .../data/readSynthesizedObject.qml | 9 ++++++ .../qdeclarativeproperty/qdeclarativeproperty.pro | 2 ++ .../tst_qdeclarativeproperty.cpp | 37 ++++++++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativeproperty/data/TestType.qml create mode 100644 tests/auto/declarative/qdeclarativeproperty/data/readSynthesizedObject.qml diff --git a/src/declarative/qml/qdeclarativeproperty.cpp b/src/declarative/qml/qdeclarativeproperty.cpp index e1ec2cd..521c241 100644 --- a/src/declarative/qml/qdeclarativeproperty.cpp +++ b/src/declarative/qml/qdeclarativeproperty.cpp @@ -793,7 +793,7 @@ QVariant QDeclarativeProperty::read(QObject *object, const QString &name, QDecla QVariant QDeclarativePropertyPrivate::readValueProperty() { - if(isValueType()) { + if (isValueType()) { QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context); QDeclarativeValueType *valueType = 0; @@ -809,13 +809,20 @@ QVariant QDeclarativePropertyPrivate::readValueProperty() if (!ep) delete valueType; return rv; - } else if(core.flags & QDeclarativePropertyCache::Data::IsQList) { + } else if (core.flags & QDeclarativePropertyCache::Data::IsQList) { QDeclarativeListProperty prop; void *args[] = { &prop, 0 }; QMetaObject::metacall(object, QMetaObject::ReadProperty, core.coreIndex, args); return QVariant::fromValue(QDeclarativeListReferencePrivate::init(prop, core.propType, engine)); + } else if (core.flags & QDeclarativePropertyCache::Data::IsQObjectDerived) { + + QObject *rv = 0; + void *args[] = { &rv, 0 }; + QMetaObject::metacall(object, QMetaObject::ReadProperty, core.coreIndex, args); + return QVariant::fromValue(rv); + } else { return object->metaObject()->property(core.coreIndex).read(object.data()); diff --git a/tests/auto/declarative/qdeclarativeproperty/data/TestType.qml b/tests/auto/declarative/qdeclarativeproperty/data/TestType.qml new file mode 100644 index 0000000..1dfb3e1 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeproperty/data/TestType.qml @@ -0,0 +1,6 @@ +import Qt 4.6 + +QtObject { + property int a: 10 +} + diff --git a/tests/auto/declarative/qdeclarativeproperty/data/readSynthesizedObject.qml b/tests/auto/declarative/qdeclarativeproperty/data/readSynthesizedObject.qml new file mode 100644 index 0000000..8085db2 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeproperty/data/readSynthesizedObject.qml @@ -0,0 +1,9 @@ +import Qt 4.6 + +QtObject { + property TestType test + + test: TestType { + property int b: 19 + } +} diff --git a/tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro b/tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro index 22e50cc..af1e1b6 100644 --- a/tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro +++ b/tests/auto/declarative/qdeclarativeproperty/qdeclarativeproperty.pro @@ -3,3 +3,5 @@ contains(QT_CONFIG,declarative): QT += declarative macx:CONFIG -= app_bundle SOURCES += tst_qdeclarativeproperty.cpp + +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp b/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp index 0333d98..9b8a643 100644 --- a/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp +++ b/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp @@ -46,6 +46,14 @@ #include #include #include +#include +#include + +inline QUrl TEST_FILE(const QString &filename) +{ + QFileInfo fileInfo(__FILE__); + return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(QLatin1String("data/") + filename)); +} class MyQmlObject : public QObject { @@ -181,6 +189,7 @@ class PropertyObject : public QObject Q_PROPERTY(QUrl url READ url WRITE setUrl); Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty); Q_PROPERTY(int propertyWithNotify READ propertyWithNotify WRITE setPropertyWithNotify NOTIFY oddlyNamedNotifySignal) + Q_PROPERTY(MyQmlObject *qmlObject READ qmlObject); Q_CLASSINFO("DefaultProperty", "defaultProperty"); public: @@ -202,6 +211,7 @@ public: int propertyWithNotify() const { return m_propertyWithNotify; } void setPropertyWithNotify(int i) { m_propertyWithNotify = i; emit oddlyNamedNotifySignal(); } + MyQmlObject *qmlObject() { return &m_qmlObject; } signals: void clicked(); void oddlyNamedNotifySignal(); @@ -211,6 +221,7 @@ private: QRect m_rect; QUrl m_url; int m_propertyWithNotify; + MyQmlObject m_qmlObject; }; QML_DECLARE_TYPE(PropertyObject); @@ -953,6 +964,32 @@ void tst_qdeclarativeproperty::read() QCOMPARE(p.read(), QVariant()); } + // Object property + { + PropertyObject o; + QDeclarativeProperty p(&o, "qmlObject"); + QCOMPARE(p.propertyTypeCategory(), QDeclarativeProperty::Object); + QCOMPARE(p.propertyType(), qMetaTypeId()); + QVariant v = p.read(); + QVERIFY(v.userType() == QMetaType::QObjectStar); + QVERIFY(qvariant_cast(v) == o.qmlObject()); + } + { + QDeclarativeComponent component(&engine, TEST_FILE("readSynthesizedObject.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QDeclarativeProperty p(object, "test", &engine); + + QCOMPARE(p.propertyTypeCategory(), QDeclarativeProperty::Object); + QVERIFY(p.propertyType() != QMetaType::QObjectStar); + + QVariant v = p.read(); + QVERIFY(v.userType() == QMetaType::QObjectStar); + QCOMPARE(qvariant_cast(v)->property("a").toInt(), 10); + QCOMPARE(qvariant_cast(v)->property("b").toInt(), 19); + } + // Attached property { QDeclarativeComponent component(&engine); -- cgit v0.12