From 4253f11774ed113cfc69794435e7e66b373bc2cd Mon Sep 17 00:00:00 2001 From: Tobias Koenig Date: Thu, 3 Sep 2015 15:55:54 +0200 Subject: Implement QQmlListModel::setData() Extending QQmlListModel by setData allows us to modify the content of the ListModel from within a delegate by doing an 'model.someProp = someValue' assignment. Change-Id: I87e4c31aca3813f099b2a4fd694beb2492a03bd0 Reviewed-by: Alan Alpert --- src/qml/types/qqmllistmodel.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/qml/types/qqmllistmodel.cpp') diff --git a/src/qml/types/qqmllistmodel.cpp b/src/qml/types/qqmllistmodel.cpp index 799f7a0b8a..da7adf4e0e 100644 --- a/src/qml/types/qqmllistmodel.cpp +++ b/src/qml/types/qqmllistmodel.cpp @@ -1875,6 +1875,30 @@ QVariant QQmlListModel::data(const QModelIndex &index, int role) const return data(index.row(), role); } +bool QQmlListModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + const int row = index.row(); + if (row >= count() || row < 0) + return false; + + if (m_dynamicRoles) { + const QByteArray property = m_roles.at(role).toUtf8(); + if (m_modelObjects[row]->setValue(property, value)) { + emitItemsChanged(row, 1, QVector() << role); + return true; + } + } else { + const ListLayout::Role &r = m_listModel->getExistingRole(role); + const int roleIndex = m_listModel->setOrCreateProperty(row, r.name, value); + if (roleIndex != -1) { + emitItemsChanged(row, 1, QVector() << role); + return true; + } + } + + return false; +} + QVariant QQmlListModel::data(int index, int role) const { QVariant v; -- cgit v1.2.3