Update roleNames in QAbstractProxyModel if sourceModel resets.

If a sourceModel resets, it's roleNames might have changed. This
is most likely the case if sourceModel itself is also a proxy model
of which the sourceModel was changed.

Task-number: QTBUG-28982
Change-Id: I102788f2c9bf97b4002b350673f9219e32e7a052
Reviewed-by: Nils Jeisecke <jeisecke@saltation.de>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Nils Jeisecke 2013-01-07 16:54:33 +01:00 committed by The Qt Project
parent 36f64ec6ac
commit ffea8e98f7
3 changed files with 53 additions and 0 deletions

View File

@ -147,6 +147,15 @@ void QAbstractProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
}
}
/*!
Clears the roleNames of this proxy model.
*/
void QAbstractProxyModel::resetInternalData()
{
Q_D(QAbstractProxyModel);
d->roleNames = d->model->roleNames();
}
/*!
Returns the model that contains the data that is available through the proxy model.
*/

View File

@ -101,6 +101,9 @@ Q_SIGNALS:
#endif
);
protected Q_SLOTS:
void resetInternalData();
protected:
QAbstractProxyModel(QAbstractProxyModelPrivate &, QObject *parent);

View File

@ -146,6 +146,8 @@ private slots:
void hierarchyFilterInvalidation();
void simpleFilterInvalidation();
void chainedProxyModelRoleNames();
protected:
void buildHierarchy(const QStringList &data, QAbstractItemModel *model);
void checkHierarchy(const QStringList &data, const QAbstractItemModel *model);
@ -3767,6 +3769,45 @@ void tst_QSortFilterProxyModel::simpleFilterInvalidation()
model.insertRow(0, new QStandardItem("extra"));
}
class CustomRoleNameModel : public QAbstractListModel
{
Q_OBJECT
public:
CustomRoleNameModel(QObject *parent = 0) : QAbstractListModel(parent) {}
QVariant data(const QModelIndex &index, int role) const
{
Q_UNUSED(index);
Q_UNUSED(role);
return QVariant();
}
int rowCount(const QModelIndex &parent = QModelIndex()) const
{
Q_UNUSED(parent);
return 0;
}
QHash<int, QByteArray> roleNames() const
{
QHash<int, QByteArray> rn = QAbstractListModel::roleNames();
rn[Qt::UserRole + 1] = "custom";
return rn;
}
};
void tst_QSortFilterProxyModel::chainedProxyModelRoleNames()
{
QSortFilterProxyModel proxy1;
QSortFilterProxyModel proxy2;
CustomRoleNameModel customModel;
proxy2.setSourceModel(&proxy1);
// changing the sourceModel of proxy1 must also update roleNames of proxy2
proxy1.setSourceModel(&customModel);
QVERIFY(proxy2.roleNames().value(Qt::UserRole + 1) == "custom");
}
QTEST_MAIN(tst_QSortFilterProxyModel)
#include "tst_qsortfilterproxymodel.moc"