diff options
-rw-r--r-- | src/declarative/qml/qdeclarativecontext.cpp | 20 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp | 31 |
2 files changed, 50 insertions, 1 deletions
diff --git a/src/declarative/qml/qdeclarativecontext.cpp b/src/declarative/qml/qdeclarativecontext.cpp index 60fc1d1..782c0d7 100644 --- a/src/declarative/qml/qdeclarativecontext.cpp +++ b/src/declarative/qml/qdeclarativecontext.cpp @@ -242,7 +242,15 @@ QObject *QDeclarativeContext::contextObject() const void QDeclarativeContext::setContextObject(QObject *object) { Q_D(QDeclarativeContext); - d->data->contextObject = object; + + QDeclarativeContextData *data = d->data; + + if (data->isInternal) { + qWarning("QDeclarativeContext: Cannot set context object for internal context."); + return; + } + + data->contextObject = object; } /*! @@ -256,6 +264,11 @@ void QDeclarativeContext::setContextProperty(const QString &name, const QVariant QDeclarativeContextData *data = d->data; + if (data->isInternal) { + qWarning("QDeclarativeContext: Cannot set property on internal context."); + return; + } + if (data->engine) { bool ok; QObject *o = QDeclarativeEnginePrivate::get(data->engine)->toQObject(value, &ok); @@ -292,6 +305,11 @@ void QDeclarativeContext::setContextProperty(const QString &name, QObject *value QDeclarativeContextData *data = d->data; + if (data->isInternal) { + qWarning("QDeclarativeContext: Cannot set property on internal context."); + return; + } + if (!data->propertyNames) data->propertyNames = new QDeclarativeIntegerCache(data->engine); int idx = data->propertyNames->value(name); diff --git a/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp b/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp index b212820..be20ba1 100644 --- a/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp +++ b/tests/auto/declarative/qdeclarativecontext/tst_qdeclarativecontext.cpp @@ -61,6 +61,7 @@ private slots: void setContextObject(); void destruction(); void idAsContextProperty(); + void readOnlyContexts(); private: QDeclarativeEngine engine; @@ -427,6 +428,36 @@ void tst_qdeclarativecontext::idAsContextProperty() delete obj; } +// Internal contexts should be read-only +void tst_qdeclarativecontext::readOnlyContexts() +{ + QDeclarativeComponent component(&engine); + component.setData("import Qt 4.6; QtObject { id: me }", QUrl()); + + QObject *obj = component.create(); + QVERIFY(obj); + + QDeclarativeContext *context = qmlContext(obj); + QVERIFY(context); + + QVERIFY(qvariant_cast<QObject*>(context->contextProperty("me")) == obj); + QVERIFY(context->contextObject() == obj); + + QTest::ignoreMessage(QtWarningMsg, "QDeclarativeContext: Cannot set property on internal context."); + context->setContextProperty("hello", 12); + QVERIFY(context->contextProperty("hello") == QVariant()); + + QTest::ignoreMessage(QtWarningMsg, "QDeclarativeContext: Cannot set property on internal context."); + context->setContextProperty("hello", obj); + QVERIFY(context->contextProperty("hello") == QVariant()); + + QTest::ignoreMessage(QtWarningMsg, "QDeclarativeContext: Cannot set context object for internal context."); + context->setContextObject(0); + QVERIFY(context->contextObject() == obj); + + delete obj; +} + QTEST_MAIN(tst_qdeclarativecontext) #include "tst_qdeclarativecontext.moc" |