diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-01-28 02:18:16 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-01-28 02:18:16 (GMT) |
commit | 56130de61b2e9e65d09ca4feef2749deea0bb4bb (patch) | |
tree | 684e456eb4e7f9affe99d5d08f03304a99642623 | |
parent | 3903deaad92f7c4c9e9258a86aeb50c5adca41c8 (diff) | |
download | Qt-56130de61b2e9e65d09ca4feef2749deea0bb4bb.zip Qt-56130de61b2e9e65d09ca4feef2749deea0bb4bb.tar.gz Qt-56130de61b2e9e65d09ca4feef2749deea0bb4bb.tar.bz2 |
Assigning undefined resets QObject properties
5 files changed, 65 insertions, 6 deletions
diff --git a/src/declarative/qml/qmlbinding.cpp b/src/declarative/qml/qmlbinding.cpp index c0389a8..3e29a3c 100644 --- a/src/declarative/qml/qmlbinding.cpp +++ b/src/declarative/qml/qmlbinding.cpp @@ -147,7 +147,11 @@ void QmlBinding::update(QmlMetaProperty::WriteFlags flags) bool isUndefined = false; QVariant value = this->value(&isUndefined); - if (isUndefined && !data->error.isValid()) { + if (isUndefined && !data->error.isValid() && data->property.isResettable()) { + + data->property.reset(); + + } else if (isUndefined && !data->error.isValid()) { QUrl url = QUrl(data->url); int line = data->line; @@ -159,7 +163,7 @@ void QmlBinding::update(QmlMetaProperty::WriteFlags flags) data->error.setDescription(QLatin1String("Unable to assign [undefined] to ") + QLatin1String(QMetaType::typeName(data->property.propertyType()))); } else if (!isUndefined && data->property.object() && - !data->property.write(value, flags)) { + !data->property.write(value, flags)) { QUrl url = QUrl(data->url); int line = data->line; diff --git a/src/declarative/qml/qmlobjectscriptclass.cpp b/src/declarative/qml/qmlobjectscriptclass.cpp index 86f0afc..7194f33 100644 --- a/src/declarative/qml/qmlobjectscriptclass.cpp +++ b/src/declarative/qml/qmlobjectscriptclass.cpp @@ -352,12 +352,18 @@ void QmlObjectScriptClass::setProperty(QObject *obj, evalContext = enginePriv->contextClass->contextFromValue(scopeNode); } - // ### Can well known types be optimized? - QVariant v = QmlScriptClass::toVariant(engine, value); QmlAbstractBinding *delBinding = QmlMetaPropertyPrivate::setBinding(obj, *lastData, 0); if (delBinding) delBinding->destroy(); - QmlMetaPropertyPrivate::write(obj, *lastData, v, evalContext); + + if (value.isUndefined() && lastData->flags & QmlPropertyCache::Data::IsResettable) { + void *a[] = { 0 }; + QMetaObject::metacall(obj, QMetaObject::ResetProperty, lastData->coreIndex, a); + } else { + // ### Can well known types be optimized? + QVariant v = QmlScriptClass::toVariant(engine, value); + QmlMetaPropertyPrivate::write(obj, *lastData, v, evalContext); + } } bool QmlObjectScriptClass::isQObject() const diff --git a/tests/auto/declarative/qmlecmascript/data/undefinedResetsProperty.qml b/tests/auto/declarative/qmlecmascript/data/undefinedResetsProperty.qml new file mode 100644 index 0000000..eceff60 --- /dev/null +++ b/tests/auto/declarative/qmlecmascript/data/undefinedResetsProperty.qml @@ -0,0 +1,7 @@ +import Qt.test 1.0 + +MyQmlObject { + property bool setUndefined: false + + resettableProperty: setUndefined?undefined:92 +} diff --git a/tests/auto/declarative/qmlecmascript/testtypes.h b/tests/auto/declarative/qmlecmascript/testtypes.h index fff9246..164dd17 100644 --- a/tests/auto/declarative/qmlecmascript/testtypes.h +++ b/tests/auto/declarative/qmlecmascript/testtypes.h @@ -73,9 +73,10 @@ class MyQmlObject : public QObject Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectChanged) Q_PROPERTY(QmlList<QObject *> *objectQmlListProperty READ objectQmlListProperty CONSTANT) Q_PROPERTY(QList<QObject *> *objectListProperty READ objectListProperty CONSTANT) + Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty) public: - MyQmlObject(): m_methodCalled(false), m_methodIntCalled(false), m_object(0), m_value(0) {} + MyQmlObject(): m_methodCalled(false), m_methodIntCalled(false), m_object(0), m_value(0), m_resetProperty(13) {} enum MyEnum { EnumValue1 = 0, EnumValue2 = 1 }; enum MyEnum2 { EnumValue3 = 2, EnumValue4 = 3 }; @@ -117,6 +118,11 @@ public: int value() const { return m_value; } void setValue(int v) { m_value = v; } + + int resettableProperty() const { return m_resetProperty; } + void setResettableProperty(int v) { m_resetProperty = v; } + void resetProperty() { m_resetProperty = 13; } + signals: void basicSignal(); void argumentSignal(int a, QString b, qreal c); @@ -141,6 +147,7 @@ private: QmlConcreteList<QObject *> m_objectQmlList; QList<QObject *> m_objectQList; int m_value; + int m_resetProperty; }; QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES) diff --git a/tests/auto/declarative/qmlecmascript/tst_qmlecmascript.cpp b/tests/auto/declarative/qmlecmascript/tst_qmlecmascript.cpp index 4c9721a..82a2351 100644 --- a/tests/auto/declarative/qmlecmascript/tst_qmlecmascript.cpp +++ b/tests/auto/declarative/qmlecmascript/tst_qmlecmascript.cpp @@ -116,6 +116,7 @@ private slots: void externalScript(); void compositePropertyType(); void jsObject(); + void undefinedResetsProperty(); void bug1(); @@ -1075,6 +1076,40 @@ void tst_qmlecmascript::jsObject() delete object; } +void tst_qmlecmascript::undefinedResetsProperty() +{ + { + QmlComponent component(&engine, TEST_FILE("undefinedResetsProperty.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("resettableProperty").toInt(), 92); + + object->setProperty("setUndefined", true); + + QCOMPARE(object->property("resettableProperty").toInt(), 13); + + object->setProperty("setUndefined", false); + + QCOMPARE(object->property("resettableProperty").toInt(), 92); + + delete object; + } + { + QmlComponent component(&engine, TEST_FILE("undefinedResetsProperty.2.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("resettableProperty").toInt(), 19); + + QMetaObject::invokeMethod(object, "doReset"); + + QCOMPARE(object->property("resettableProperty").toInt(), 13); + + delete object; + } +} + // QTBUG-6781 void tst_qmlecmascript::bug1() { |