Don't bypass overwritten [set]data() methods in the proxy.

By calling itemData() of the source model directly, the result cannot
contain data provided by the proxy model itself. The base class
implementation however will call data() on the proxy instead.

Change-Id: Ib0ef5f5621457adbfa4bd896a756dfcb98d0ae54
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Volker Krause 2013-04-08 14:14:32 +02:00 committed by The Qt Project
parent dce86de8e7
commit 96e3c2bcbf
2 changed files with 28 additions and 4 deletions

View File

@ -272,8 +272,7 @@ QVariant QAbstractProxyModel::headerData(int section, Qt::Orientation orientatio
*/
QMap<int, QVariant> QAbstractProxyModel::itemData(const QModelIndex &proxyIndex) const
{
Q_D(const QAbstractProxyModel);
return d->model->itemData(mapToSource(proxyIndex));
return QAbstractItemModel::itemData(proxyIndex);
}
/*!
@ -299,8 +298,7 @@ bool QAbstractProxyModel::setData(const QModelIndex &index, const QVariant &valu
*/
bool QAbstractProxyModel::setItemData(const QModelIndex &index, const QMap< int, QVariant >& roles)
{
Q_D(QAbstractProxyModel);
return d->model->setItemData(mapToSource(index), roles);
return QAbstractItemModel::setItemData(index, roles);
}
/*!

View File

@ -79,6 +79,8 @@ private slots:
void reset();
void dataChanged();
void itemData();
protected:
void verifyIdentity(QAbstractItemModel *model, const QModelIndex &parent = QModelIndex());
@ -364,5 +366,29 @@ void tst_QIdentityProxyModel::dataChanged()
m_proxy->setSourceModel(0);
}
class AppendStringProxy : public QIdentityProxyModel
{
public:
QVariant data(const QModelIndex &index, int role) const
{
const QVariant result = sourceModel()->data(index, role);
if (role != Qt::DisplayRole)
return result;
return result.toString() + "_appended";
}
};
void tst_QIdentityProxyModel::itemData()
{
QStringListModel model(QStringList() << "Monday" << "Tuesday" << "Wednesday");
AppendStringProxy proxy;
proxy.setSourceModel(&model);
const QModelIndex topIndex = proxy.index(0, 0);
QCOMPARE(topIndex.data(Qt::DisplayRole).toString(), QStringLiteral("Monday_appended"));
QCOMPARE(proxy.data(topIndex, Qt::DisplayRole).toString(), QStringLiteral("Monday_appended"));
QCOMPARE(proxy.itemData(topIndex).value(Qt::DisplayRole).toString(), QStringLiteral("Monday_appended"));
}
QTEST_MAIN(tst_QIdentityProxyModel)
#include "tst_qidentityproxymodel.moc"