From 9f9b23fd7943a3d125cb1cc9f333ce430b2706ea Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Mon, 25 Jul 2011 16:03:57 +1000 Subject: [PATCH] Use engine where appropriate in QDeclarativeProperty Task-number: QTBUG-14697 Change-Id: Id15def75271666699b1fe23e39991710afebff59 Reviewed-on: http://codereview.qt.nokia.com/2077 Reviewed-by: Aaron Kennedy --- src/declarative/qml/qdeclarativeproperty.cpp | 17 ++++++++++++----- src/declarative/qml/qdeclarativeproperty_p.h | 1 + .../tst_qdeclarativeproperty.cpp | 18 +++++++++++++++++- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/declarative/qml/qdeclarativeproperty.cpp b/src/declarative/qml/qdeclarativeproperty.cpp index 9b7431a..98c7582 100644 --- a/src/declarative/qml/qdeclarativeproperty.cpp +++ b/src/declarative/qml/qdeclarativeproperty.cpp @@ -213,6 +213,13 @@ QDeclarativeProperty::QDeclarativeProperty(QObject *obj, const QString &name, QD Q_GLOBAL_STATIC(QDeclarativeValueTypeFactory, qmlValueTypes); +QDeclarativeContextData *QDeclarativePropertyPrivate::effectiveContext() const +{ + if (context) return context; + else if (engine) return QDeclarativeContextData::get(engine->rootContext()); + else return 0; +} + void QDeclarativePropertyPrivate::initProperty(QObject *obj, const QString &name) { if (!obj) return; @@ -392,7 +399,7 @@ const char *QDeclarativeProperty::propertyTypeName() const return 0; if (d->isValueType()) { - QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(d->context); + QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(d->engine); QDeclarativeValueType *valueType = 0; if (ep) valueType = ep->valueTypes[d->core.propType]; else valueType = QDeclarativeValueTypeFactory::valueType(d->core.propType); @@ -980,7 +987,7 @@ QVariant QDeclarativePropertyPrivate::readValueProperty() { if (isValueType()) { - QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context); + QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); QDeclarativeValueType *valueType = 0; if (ep) valueType = ep->valueTypes[core.propType]; else valueType = QDeclarativeValueTypeFactory::valueType(core.propType); @@ -1065,7 +1072,7 @@ bool QDeclarativePropertyPrivate::writeValueProperty(const QVariant &value, Writ bool rv = false; if (isValueType()) { - QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context); + QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); QDeclarativeValueType *writeBack = 0; if (ep) { @@ -1080,14 +1087,14 @@ bool QDeclarativePropertyPrivate::writeValueProperty(const QVariant &value, Writ data.setFlags(valueType.flags); data.coreIndex = valueType.valueTypeCoreIdx; data.propType = valueType.valueTypePropType; - rv = write(writeBack, data, value, context, flags); + rv = write(writeBack, data, value, effectiveContext(), flags); writeBack->write(object, core.coreIndex, flags); if (!ep) delete writeBack; } else { - rv = write(object, core, value, context, flags); + rv = write(object, core, value, effectiveContext(), flags); } diff --git a/src/declarative/qml/qdeclarativeproperty_p.h b/src/declarative/qml/qdeclarativeproperty_p.h index ea3fb56..d05e155 100644 --- a/src/declarative/qml/qdeclarativeproperty_p.h +++ b/src/declarative/qml/qdeclarativeproperty_p.h @@ -75,6 +75,7 @@ public: QDeclarativePropertyPrivate() : context(0), engine(0), object(0), isNameCached(false) {} + inline QDeclarativeContextData *effectiveContext() const; QDeclarativeContextData *context; QDeclarativeEngine *engine; QDeclarativeGuard object; diff --git a/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp b/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp index a150329..5218093 100644 --- a/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp +++ b/tests/auto/declarative/qdeclarativeproperty/tst_qdeclarativeproperty.cpp @@ -133,6 +133,7 @@ private slots: // Bugs void crashOnValueProperty(); void aliasPropertyBindings(); + void noContext(); void copy(); private: @@ -1481,6 +1482,22 @@ void tst_qdeclarativeproperty::copy() QCOMPARE(p2.propertyType(), (int)QVariant::Int); } +void tst_qdeclarativeproperty::noContext() +{ + QDeclarativeComponent compA(&engine, TEST_FILE("NoContextTypeA.qml")); + QDeclarativeComponent compB(&engine, TEST_FILE("NoContextTypeB.qml")); + + QObject *a = compA.create(); + QVERIFY(a != 0); + QObject *b = compB.create(); + QVERIFY(b != 0); + + QVERIFY(QDeclarativeProperty::write(b, "myTypeA", QVariant::fromValue(a), &engine)); + + delete a; + delete b; +} + void tst_qdeclarativeproperty::initTestCase() { qmlRegisterType("Test",1,0,"MyQmlObject"); @@ -1488,7 +1505,6 @@ void tst_qdeclarativeproperty::initTestCase() qmlRegisterType("Test",1,0,"MyContainer"); } - QTEST_MAIN(tst_qdeclarativeproperty) #include "tst_qdeclarativeproperty.moc" -- 1.7.2.5