From 6acdb3256113a5cdc9633077ada5674e5ee694d1 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Mon, 16 Jan 2012 14:51:29 +0100 Subject: [PATCH] Clarify docs that everything must be re-queried on model reset. Change-Id: I05970302d4f52d092a7c65a45b9e5a3570b1d144 Reviewed-by: Olivier Goffart --- src/corelib/itemmodels/qabstractitemmodel.cpp | 9 +++-- .../tst_qabstractitemmodel.cpp | 37 ++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index dec1fe4cef9..a2776271a81 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -2141,9 +2141,7 @@ QSize QAbstractItemModel::span(const QModelIndex &) const Sets the model's role names to \a roleNames. This function allows mapping of role identifiers to role property names in - Declarative UI. This function must be called before the model is used. - Modifying the role names after the model has been set may result in - undefined behaviour. + scripting languages. \sa roleNames() */ @@ -3420,6 +3418,11 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti This signal is emitted when reset() is called, after the model's internal state (e.g. persistent model indexes) has been invalidated. + Note that if a model is reset it should be considered that all information + previously retrieved from it is invalid. This includes but is not limited + to the rowCount() and columnCount(), flags(), data retrieved through data(), + and roleNames(). + \sa endResetModel(), modelAboutToBeReset() */ diff --git a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp index af1e492a8ba..8829f6e7b1e 100644 --- a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp +++ b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp @@ -1669,6 +1669,19 @@ private: }; +class ModelWithCustomRole : public QStringListModel +{ + Q_OBJECT +public: + ModelWithCustomRole(QObject *parent = 0) + : QStringListModel(parent) + { + QHash roleNames_ = roleNames(); + roleNames_.insert(Qt::UserRole + 1, "custom"); + setRoleNames(roleNames_); + } +}; + ListenerObject::ListenerObject(QAbstractProxyModel *parent) : QObject(parent), m_model(parent) { @@ -1722,7 +1735,7 @@ void tst_QAbstractItemModel::testReset() nullProxy->setSourceModel(m_model); // Makes sure the model and proxy are in a consistent state. before and after reset. - new ListenerObject(nullProxy); + ListenerObject *listener = new ListenerObject(nullProxy); ModelResetCommandFixed *resetCommand = new ModelResetCommandFixed(m_model, this); @@ -1741,6 +1754,28 @@ void tst_QAbstractItemModel::testReset() QVERIFY(m_model->rowCount() == 9); QModelIndex destIndex = m_model->index(4, 0); QVERIFY(m_model->rowCount(destIndex) == 11); + + // Delete it because its slots test things which are not true after this point. + delete listener; + + QSignalSpy proxyBeforeResetSpy(nullProxy, SIGNAL(modelAboutToBeReset())); + QSignalSpy proxyAfterResetSpy(nullProxy, SIGNAL(modelReset())); + + // Before setting it, it does not have custom roles. + QCOMPARE(nullProxy->roleNames().value(Qt::UserRole + 1), QByteArray()); + + nullProxy->setSourceModel(new ModelWithCustomRole(this)); + QVERIFY(proxyBeforeResetSpy.size() == 1); + QVERIFY(proxyAfterResetSpy.size() == 1); + + QCOMPARE(nullProxy->roleNames().value(Qt::UserRole + 1), QByteArray("custom")); + + nullProxy->setSourceModel(m_model); + QVERIFY(proxyBeforeResetSpy.size() == 2); + QVERIFY(proxyAfterResetSpy.size() == 2); + + // After being reset the proxy must be queried again. + QCOMPARE(nullProxy->roleNames().value(Qt::UserRole + 1), QByteArray()); } class CustomRoleModel : public QStringListModel