From 8edaa5f5d1d7b78007e07e65987e26bd843970e8 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 27 Aug 2009 13:11:38 +1000 Subject: Support using a QAbstractItemModel with Repeater. --- src/declarative/fx/qfxrepeater.cpp | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/declarative/fx/qfxrepeater.cpp b/src/declarative/fx/qfxrepeater.cpp index fa44f8f..67d4d69 100644 --- a/src/declarative/fx/qfxrepeater.cpp +++ b/src/declarative/fx/qfxrepeater.cpp @@ -157,10 +157,13 @@ QFxRepeater::~QFxRepeater() As a special case the model can also be merely a number. In this case it will create that many instances of the component. They will also be assigned an index based on the order they are created. + + Models can also be created directly in QML, using a \l{ListModel} or \l{XmlListModel}. */ QVariant QFxRepeater::model() const { - return QVariant(); + Q_D(const QFxRepeater); + return d->dataSource; } void QFxRepeater::setModel(const QVariant &v) @@ -296,6 +299,38 @@ void QFxRepeater::regenerate() if (QFxItem *item = d->addItem(ctxt, lastItem)) lastItem = item; } + } else if (QAbstractItemModel *model = qobject_cast(d->dataSource.value())) { + count = model->rowCount(); + if (count <= 0) + return; + + for (int ii = 0; ii < count; ++ii) { + QmlContext *ctxt = new QmlContext(qmlContext(this), this); + d->deletables << ctxt; + + ctxt->setContextProperty(QLatin1String("index"), ii); + + QList roles; + QStringList roleNames; + QHash data; + for (QHash::const_iterator it = model->roleNames().begin(); + it != model->roleNames().end(); ++it) { + roles.append(it.key()); + roleNames.append(QLatin1String(*it)); + } + + QModelIndex index = model->index(ii, 0); + for (int j = 0; j < roles.size(); ++j) { + ctxt->setContextProperty(roleNames.at(j), model->data(index, roles.at(j))); + } + + //for compatability with other lists, assign data if there is only a single role + if (roles.size() == 1) + ctxt->setContextProperty(QLatin1String("modelData"), data.value(roles.at(0))); + + if (QFxItem *item = d->addItem(ctxt, lastItem)) + lastItem = item; + } } else if (QObject *object = d->dataSource.value()) { // A single object (i.e. list of size 1). // Properties are the roles (excluding objectName). -- cgit v0.12