Clarify docs that everything must be re-queried on model reset.

Change-Id: I05970302d4f52d092a7c65a45b9e5a3570b1d144
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Stephen Kelly 2012-01-16 14:51:29 +01:00 committed by Qt by Nokia
parent 7a0099183a
commit 6acdb32561
2 changed files with 42 additions and 4 deletions

View File

@ -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()
*/

View File

@ -1669,6 +1669,19 @@ private:
};
class ModelWithCustomRole : public QStringListModel
{
Q_OBJECT
public:
ModelWithCustomRole(QObject *parent = 0)
: QStringListModel(parent)
{
QHash<int, QByteArray> 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