aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew den Exter <[email protected]>2011-07-28 11:17:21 +1000
committerQt by Nokia <[email protected]>2011-07-28 04:34:31 +0200
commit0e008e9a8fc32d84c03e88a4a41ec8801b274979 (patch)
tree1ffe6e0cdba0f0061b3f13b026517c458999bf6d
parent44e201c04e699f3bee49f63894477f5105092cb0 (diff)
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 <[email protected]> Reviewed-by: Martin Jones <[email protected]>
-rw-r--r--src/declarative/items/qsgvisualitemmodel.cpp11
-rw-r--r--tests/auto/declarative/qsgvisualdatamodel/tst_qsgvisualdatamodel.cpp26
2 files changed, 29 insertions, 8 deletions
diff --git a/src/declarative/items/qsgvisualitemmodel.cpp b/src/declarative/items/qsgvisualitemmodel.cpp
index 66271e13d4..7e45aeb629 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 5b53a0f1b4..e56fcb062e 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<int, QByteArray> 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<QSGListView*>(view.rootObject());
+ QVERIFY(listview != 0);
+
+ QSGItem *contentItem = listview->contentItem();
+ QVERIFY(contentItem != 0);
+
+ QSGText *name = findItem<QSGText>(contentItem, "name", 1);
+ QCOMPARE(name->text(), QString("two"));
+
+ model.set(1, "Changed");
+ QCOMPARE(name->text(), QString("Changed"));
+ }
}
void tst_qsgvisualdatamodel::modelProperties()