From c6101765924d2cc8290f342a945106405bc42129 Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 9 Nov 2009 16:23:42 +1000 Subject: Fix and test set/get of structured ListModel data. --- src/declarative/util/qmllistmodel.cpp | 33 ++++++++++++++-------- .../declarative/qmllistmodel/tst_qmllistmodel.cpp | 4 ++- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp index fbd957c..082d70d 100644 --- a/src/declarative/util/qmllistmodel.cpp +++ b/src/declarative/util/qmllistmodel.cpp @@ -221,21 +221,22 @@ struct ModelNode QList values; QHash properties; - QmlListModel *model() { + QmlListModel *model(const QmlListModel *parent) { if (!modelCache) { modelCache = new QmlListModel; + QmlEngine::setContextForObject(modelCache,QmlEngine::contextForObject(parent)); + modelCache->_root = this; } return modelCache; } - ModelObject *object() { + ModelObject *object(const QmlListModel *parent) { if (!objectCache) { objectCache = new ModelObject(); QHash::iterator it; for (it = properties.begin(); it != properties.end(); ++it) { - if (!(*it)->values.isEmpty()) - objectCache->setValue(it.key().toUtf8(), (*it)->values.first()); + objectCache->setValue(it.key().toUtf8(), parent->valueForNode(*it)); } } return objectCache; @@ -365,7 +366,7 @@ QVariant QmlListModel::valueForNode(ModelNode *node) const if (!node->properties.isEmpty()) { // Object - rv = node->object(); + rv = node->object(this); } else if (node->values.count() == 0) { // Invalid return QVariant(); @@ -375,15 +376,15 @@ QVariant QmlListModel::valueForNode(ModelNode *node) const ModelNode *valueNode = qvariant_cast(var); if (valueNode) { if (!valueNode->properties.isEmpty()) - rv = valueNode->object(); + rv = valueNode->object(this); else - rv = valueNode->model(); + rv = valueNode->model(this); } else { return var; } } else if (node->values.count() > 1) { // List - rv = node->model(); + rv = node->model(this); } if (rv) @@ -579,17 +580,27 @@ void QmlListModel::append(const QScriptValue& valuemap) } /*! - \qmlmethod dict ListModel::get(index) + \qmlmethod object ListModel::get(index) Returns the item at \a index in the list model. \code - FruitModel.append({"cost": 5.95, "name":"Pizza"}) + FruitModel.append({"cost": 5.95, "name":"Jackfruit"}) FruitModel.get(0).cost \endcode The \a index must be an element in the list. + Note that properties of the returned object that are themselves objects + will also be models, and this get() method is used to access elements: + + \code + FruitModel.append(..., "attributes": + [{"name":"spikes","value":"7mm"}, + {"name":"color","value":"green"}]); + FruitModel.get(0).attributes.get(1).value; // == "green" + \endcode + \sa append() */ QScriptValue QmlListModel::get(int index) const @@ -607,7 +618,7 @@ QScriptValue QmlListModel::get(int index) const qWarning("Cannot call QmlListModel::get() without a QmlEngine"); return 0; } - return QmlEnginePrivate::qmlScriptObject(node->object(), eng); + return QmlEnginePrivate::qmlScriptObject(node->object(this), eng); } /*! diff --git a/tests/auto/declarative/qmllistmodel/tst_qmllistmodel.cpp b/tests/auto/declarative/qmllistmodel/tst_qmllistmodel.cpp index 9ce1a7c..3222d42 100644 --- a/tests/auto/declarative/qmllistmodel/tst_qmllistmodel.cpp +++ b/tests/auto/declarative/qmllistmodel/tst_qmllistmodel.cpp @@ -110,7 +110,9 @@ void tst_QmlListModel::dynamic_data() // Structured model - // XXX todo + QTest::newRow("listprop1a") << "{append({'foo':123,'bars':[{'a':1},{'a':2},{'a':3}]});count}" << 1 << ""; + QTest::newRow("listprop1b") << "{append({'foo':123,'bars':[{'a':1},{'a':2},{'a':3}]});get(0).bars.get(1).a}" << 2 << ""; + QTest::newRow("listprop2a") << "{append({'foo':123,'bars':[{'a':1},{'a':2},{'a':3}]});get(0).bars.append({'a':4});get(0).bars.get(3).a}" << 4 << ""; } void tst_QmlListModel::dynamic() -- cgit v0.12 From 813cf7f88015680206aafb1d9dfd05900e64abbd Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 9 Nov 2009 16:42:55 +1000 Subject: ListModel elements can only be values or *lists* of objects, not objects. Fixes QTBUG-5495 --- src/declarative/util/qmllistmodel.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp index 082d70d..748dca6 100644 --- a/src/declarative/util/qmllistmodel.cpp +++ b/src/declarative/util/qmllistmodel.cpp @@ -276,8 +276,6 @@ void ModelNode::setObjectValue(const QScriptValue& valuemap) { QScriptValue v = it.value(); if (v.isArray()) { value->setListValue(v); - } else if (v.isObject()) { - value->setObjectValue(v); } else { value->values << v.toVariant(); } -- cgit v0.12