diff options
author | Bea Lam <bea.lam@nokia.com> | 2010-05-20 06:42:06 (GMT) |
---|---|---|
committer | Bea Lam <bea.lam@nokia.com> | 2010-05-21 00:11:34 (GMT) |
commit | 30b490476bad5d486332093fc672856a8dd6a195 (patch) | |
tree | d33650d24441fd2dc4d91076fca6821aedcb9d83 /src/declarative/util | |
parent | 0a2ef85bc7c560f38f5ba591968ce49969893a1c (diff) | |
download | Qt-30b490476bad5d486332093fc672856a8dd6a195.zip Qt-30b490476bad5d486332093fc672856a8dd6a195.tar.gz Qt-30b490476bad5d486332093fc672856a8dd6a195.tar.bz2 |
Add XmlListModel::get()
Task-number: QTBUG-10761
Diffstat (limited to 'src/declarative/util')
-rw-r--r-- | src/declarative/util/qdeclarativexmllistmodel.cpp | 38 | ||||
-rw-r--r-- | src/declarative/util/qdeclarativexmllistmodel_p.h | 3 |
2 files changed, 40 insertions, 1 deletions
diff --git a/src/declarative/util/qdeclarativexmllistmodel.cpp b/src/declarative/util/qdeclarativexmllistmodel.cpp index cae1d3a..ae9b323 100644 --- a/src/declarative/util/qdeclarativexmllistmodel.cpp +++ b/src/declarative/util/qdeclarativexmllistmodel.cpp @@ -42,7 +42,7 @@ #include "private/qdeclarativexmllistmodel_p.h" #include <qdeclarativecontext.h> -#include <qdeclarativeengine.h> +#include <qdeclarativeengine_p.h> #include <QDebug> #include <QStringList> @@ -727,6 +727,42 @@ void QDeclarativeXmlListModel::setNamespaceDeclarations(const QString &declarati } /*! + \qmlmethod object XmlListModel::get(int index) + + Returns the item at \a index in the model. + + For example, for a model like this: + + \qml + XmlListModel { + id: model + source: "http://mysite.com/feed.xml" + query: "/feed/entry" + XmlRole { name: "title"; query: "title/string()" } + } + \qml + + This will access the \c title value for the first item in the model: + + \qml + var title = model.get(0).title; + \qml +*/ +QScriptValue QDeclarativeXmlListModel::get(int index) const +{ + Q_D(const QDeclarativeXmlListModel); + + QScriptEngine *sengine = QDeclarativeEnginePrivate::getScriptEngine(qmlContext(this)->engine()); + if (index < 0 || index >= count()) + return sengine->undefinedValue(); + + QScriptValue sv = sengine->newObject(); + for (int i=0; i<d->roleObjects.count(); i++) + sv.setProperty(d->roleObjects[i]->name(), qScriptValueFromValue(sengine, d->data.value(i).value(index))); + return sv; +} + +/*! \qmlproperty enumeration XmlListModel::status Specifies the model loading status, which can be one of the following: diff --git a/src/declarative/util/qdeclarativexmllistmodel_p.h b/src/declarative/util/qdeclarativexmllistmodel_p.h index 7101c57..4460f24 100644 --- a/src/declarative/util/qdeclarativexmllistmodel_p.h +++ b/src/declarative/util/qdeclarativexmllistmodel_p.h @@ -47,6 +47,7 @@ #include <QtCore/qurl.h> #include <QtCore/qstringlist.h> +#include <QtScript/qscriptvalue.h> #include <private/qlistmodelinterface_p.h> @@ -109,6 +110,8 @@ public: QString namespaceDeclarations() const; void setNamespaceDeclarations(const QString&); + Q_INVOKABLE QScriptValue get(int index) const; + enum Status { Null, Ready, Loading, Error }; Status status() const; qreal progress() const; |