summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/declarative/util/qmllistmodel.cpp35
-rw-r--r--tests/auto/declarative/qmllistmodel/tst_qmllistmodel.cpp4
2 files changed, 25 insertions, 14 deletions
diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp
index fbd957c..748dca6 100644
--- a/src/declarative/util/qmllistmodel.cpp
+++ b/src/declarative/util/qmllistmodel.cpp
@@ -221,21 +221,22 @@ struct ModelNode
QList<QVariant> values;
QHash<QString, ModelNode *> 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<QString, ModelNode *>::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;
@@ -275,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();
}
@@ -365,7 +364,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 +374,15 @@ QVariant QmlListModel::valueForNode(ModelNode *node) const
ModelNode *valueNode = qvariant_cast<ModelNode *>(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 +578,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 +616,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()