From 0e008e9a8fc32d84c03e88a4a41ec8801b274979 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Thu, 28 Jul 2011 11:17:21 +1000 Subject: [PATCH] Fix crash when a model defines its own modelData role. Don't create a duplicate modelData property if one has already been created from the model's roles. Task-number: QTBUG-18801 Change-Id: Ic46763a9dc067cfb53b0655d991c553b3b10f023 Reviewed-on: http://codereview.qt.nokia.com/2312 Reviewed-by: Qt Sanity Bot Reviewed-by: Martin Jones --- src/declarative/items/qsgvisualitemmodel.cpp | 11 +++++---- .../qsgvisualdatamodel/tst_qsgvisualdatamodel.cpp | 26 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/declarative/items/qsgvisualitemmodel.cpp b/src/declarative/items/qsgvisualitemmodel.cpp index 66271e1..7e45aeb 100644 --- a/src/declarative/items/qsgvisualitemmodel.cpp +++ b/src/declarative/items/qsgvisualitemmodel.cpp @@ -270,7 +270,7 @@ public: ++it; } // Add modelData property - if (m_roles.count() == 1) + if (m_roles.count() == 1 && !m_roleNames.contains("modelData")) m_modelDataPropId = m_delegateDataType->createProperty("modelData") - m_delegateDataType->propertyOffset(); m_metaDataCreated = true; } @@ -1061,16 +1061,15 @@ void QSGVisualDataModel::_q_itemsChanged(int index, int count, qmlInfo(this) << "Changing role not present in item: " << roleName; } } - if (d->m_roles.count() == 1) { + if (d->m_modelDataPropId != -1) { // Handle the modelData role we add if there is just one role. - int propId = data->modelDataPropertyId(); - if (data->hasValue(propId)) { + if (data->hasValue(d->m_modelDataPropId)) { int role = d->m_roles.at(0); if (d->m_listModelInterface) { - data->setValue(propId, d->m_listModelInterface->data(idx, role)); + data->setValue(d->m_modelDataPropId, d->m_listModelInterface->data(idx, role)); } else if (d->m_abstractItemModel) { QModelIndex index = d->m_abstractItemModel->index(idx, 0, d->m_root); - data->setValue(propId, d->m_abstractItemModel->data(index, role)); + data->setValue(d->m_modelDataPropId, d->m_abstractItemModel->data(index, role)); } } } diff --git a/tests/auto/declarative/qsgvisualdatamodel/tst_qsgvisualdatamodel.cpp b/tests/auto/declarative/qsgvisualdatamodel/tst_qsgvisualdatamodel.cpp index 5b53a0f..e56fcb0 100644 --- a/tests/auto/declarative/qsgvisualdatamodel/tst_qsgvisualdatamodel.cpp +++ b/tests/auto/declarative/qsgvisualdatamodel/tst_qsgvisualdatamodel.cpp @@ -81,9 +81,9 @@ class SingleRoleModel : public QAbstractListModel Q_OBJECT public: - SingleRoleModel(QObject *parent = 0) { + SingleRoleModel(const QByteArray &role = "name", QObject *parent = 0) { QHash roles; - roles.insert(Qt::DisplayRole , "name"); + roles.insert(Qt::DisplayRole , role); setRoleNames(roles); list << "one" << "two" << "three" << "four"; } @@ -382,6 +382,28 @@ void tst_qsgvisualdatamodel::singleRole() model.set(1, "Changed"); QCOMPARE(name->text(), QString("Changed")); } + { + QSGView view; + + SingleRoleModel model("modelData"); + + QDeclarativeContext *ctxt = view.rootContext(); + ctxt->setContextProperty("myModel", &model); + + view.setSource(QUrl::fromLocalFile(SRCDIR "/data/singlerole2.qml")); + + QSGListView *listview = qobject_cast(view.rootObject()); + QVERIFY(listview != 0); + + QSGItem *contentItem = listview->contentItem(); + QVERIFY(contentItem != 0); + + QSGText *name = findItem(contentItem, "name", 1); + QCOMPARE(name->text(), QString("two")); + + model.set(1, "Changed"); + QCOMPARE(name->text(), QString("Changed")); + } } void tst_qsgvisualdatamodel::modelProperties() -- 2.7.4