diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-20 04:49:36 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-04-20 04:49:36 (GMT) |
commit | 78c78085449149b5c48bbecd49424974cdf79bee (patch) | |
tree | c56b323d7270bb3515d2d7277e366a8fa2e76e34 | |
parent | 27b09c283d68fa4b1eae32f82a1041bca1a28656 (diff) | |
download | Qt-78c78085449149b5c48bbecd49424974cdf79bee.zip Qt-78c78085449149b5c48bbecd49424974cdf79bee.tar.gz Qt-78c78085449149b5c48bbecd49424974cdf79bee.tar.bz2 |
Allow null to be assigned to object properties
5 files changed, 61 insertions, 1 deletions
diff --git a/src/declarative/qml/qdeclarativebinding.cpp b/src/declarative/qml/qdeclarativebinding.cpp index d759427..95520da 100644 --- a/src/declarative/qml/qdeclarativebinding.cpp +++ b/src/declarative/qml/qdeclarativebinding.cpp @@ -156,6 +156,9 @@ void QDeclarativeBinding::update(QDeclarativePropertyPrivate::WriteFlags flags) QScriptValue scriptValue = d->scriptValue(0, &isUndefined); if (data->property.propertyTypeCategory() == QDeclarativeProperty::List) { value = ep->scriptValueToVariant(scriptValue, qMetaTypeId<QList<QObject *> >()); + } else if (scriptValue.isNull() && + data->property.propertyTypeCategory() == QDeclarativeProperty::Object) { + value = QVariant::fromValue((QObject *)0); } else { value = ep->scriptValueToVariant(scriptValue, data->property.propertyType()); if (value.userType() == QMetaType::QObjectStar && !qvariant_cast<QObject*>(value)) { @@ -168,6 +171,7 @@ void QDeclarativeBinding::update(QDeclarativePropertyPrivate::WriteFlags flags) } } + if (data->error.isValid()) { } else if (isUndefined && data->property.isResettable()) { diff --git a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp index a194354..bb5c8b7 100644 --- a/src/declarative/qml/qdeclarativeobjectscriptclass.cpp +++ b/src/declarative/qml/qdeclarativeobjectscriptclass.cpp @@ -353,7 +353,13 @@ void QDeclarativeObjectScriptClass::setProperty(QObject *obj, if (delBinding) delBinding->destroy(); - if (value.isUndefined() && lastData->flags & QDeclarativePropertyCache::Data::IsResettable) { + if (value.isNull() && lastData->flags & QDeclarativePropertyCache::Data::IsQObjectDerived) { + QObject *o = 0; + int status = -1; + int flags = 0; + void *argv[] = { &o, 0, &status, &flags }; + QMetaObject::metacall(obj, QMetaObject::WriteProperty, lastData->coreIndex, argv); + } else if (value.isUndefined() && lastData->flags & QDeclarativePropertyCache::Data::IsResettable) { void *a[] = { 0 }; QMetaObject::metacall(obj, QMetaObject::ResetProperty, lastData->coreIndex, a); } else if (value.isUndefined() && lastData->propType == qMetaTypeId<QVariant>()) { diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/canAssignNullToQObject.1.qml b/tests/auto/declarative/qdeclarativeecmascript/data/canAssignNullToQObject.1.qml new file mode 100644 index 0000000..3fd9131 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeecmascript/data/canAssignNullToQObject.1.qml @@ -0,0 +1,9 @@ +import Qt.test 1.0 + +MyQmlObject { + property bool runTest: false + + property variant a: MyQmlObject {} + + objectProperty: (runTest == false)?a:null +} diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/canAssignNullToQObject.2.qml b/tests/auto/declarative/qdeclarativeecmascript/data/canAssignNullToQObject.2.qml new file mode 100644 index 0000000..19b0c42 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeecmascript/data/canAssignNullToQObject.2.qml @@ -0,0 +1,11 @@ +import Qt.test 1.0 +import Qt 4.6 + +MyQmlObject { + objectProperty: MyQmlObject {} + + Component.onCompleted: { + objectProperty = null; + } +} + diff --git a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp index a2ecf74..6939290 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp +++ b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp @@ -141,6 +141,7 @@ private slots: void variantsAssignedUndefined(); void qtbug_9792(); void noSpuriousWarningsAtShutdown(); + void canAssignNullToQObject(); void callQtInvokables(); private: @@ -2190,6 +2191,35 @@ void tst_qdeclarativeecmascript::noSpuriousWarningsAtShutdown() } } +void tst_qdeclarativeecmascript::canAssignNullToQObject() +{ + { + QDeclarativeComponent component(&engine, TEST_FILE("canAssignNullToQObject.1.qml")); + + MyQmlObject *o = qobject_cast<MyQmlObject *>(component.create()); + QVERIFY(o != 0); + + QVERIFY(o->objectProperty() != 0); + + o->setProperty("runTest", true); + + QVERIFY(o->objectProperty() == 0); + + delete o; + } + + { + QDeclarativeComponent component(&engine, TEST_FILE("canAssignNullToQObject.2.qml")); + + MyQmlObject *o = qobject_cast<MyQmlObject *>(component.create()); + QVERIFY(o != 0); + + QVERIFY(o->objectProperty() == 0); + + delete o; + } +} + QTEST_MAIN(tst_qdeclarativeecmascript) #include "tst_qdeclarativeecmascript.moc" |