Skip to content

Commit bb00ac8

Browse files
vkrauseThe Qt Project
authored andcommitted
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. Cherry-picked from qtbase/96e3c2bcbfedc8b5cb8fc099229a02a1fa335c21. Change-Id: I7e8b65ab045382089c577d9832edc1555b71419e Reviewed-by: Stephen Kelly <[email protected]>
1 parent 66ebc3e commit bb00ac8

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/gui/itemviews/qabstractproxymodel.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ QVariant QAbstractProxyModel::headerData(int section, Qt::Orientation orientatio
248248
*/
249249
QMap<int, QVariant> QAbstractProxyModel::itemData(const QModelIndex &proxyIndex) const
250250
{
251-
Q_D(const QAbstractProxyModel);
252-
return d->model->itemData(mapToSource(proxyIndex));
251+
return QAbstractItemModel::itemData(proxyIndex);
253252
}
254253

255254
/*!
@@ -275,8 +274,7 @@ bool QAbstractProxyModel::setData(const QModelIndex &index, const QVariant &valu
275274
*/
276275
bool QAbstractProxyModel::setItemData(const QModelIndex &index, const QMap< int, QVariant >& roles)
277276
{
278-
Q_D(QAbstractProxyModel);
279-
return d->model->setItemData(mapToSource(index), roles);
277+
return QAbstractItemModel::setItemData(index, roles);
280278
}
281279

282280
/*!

tests/auto/qidentityproxymodel/tst_qidentityproxymodel.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ private slots:
7575
void moveRows();
7676
void reset();
7777

78+
void itemData();
79+
7880
protected:
7981
void verifyIdentity(QAbstractItemModel *model, const QModelIndex &parent = QModelIndex());
8082

@@ -330,5 +332,29 @@ void tst_QIdentityProxyModel::reset()
330332
m_proxy->setSourceModel(0);
331333
}
332334

335+
class AppendStringProxy : public QIdentityProxyModel
336+
{
337+
public:
338+
QVariant data(const QModelIndex &index, int role) const
339+
{
340+
const QVariant result = sourceModel()->data(index, role);
341+
if (role != Qt::DisplayRole)
342+
return result;
343+
return result.toString() + "_appended";
344+
}
345+
};
346+
347+
void tst_QIdentityProxyModel::itemData()
348+
{
349+
QStringListModel model(QStringList() << "Monday" << "Tuesday" << "Wednesday");
350+
AppendStringProxy proxy;
351+
proxy.setSourceModel(&model);
352+
353+
const QModelIndex topIndex = proxy.index(0, 0);
354+
QCOMPARE(topIndex.data(Qt::DisplayRole).toString(), QString::fromLatin1("Monday_appended"));
355+
QCOMPARE(proxy.data(topIndex, Qt::DisplayRole).toString(), QString::fromLatin1("Monday_appended"));
356+
QCOMPARE(proxy.itemData(topIndex).value(Qt::DisplayRole).toString(), QString::fromLatin1("Monday_appended"));
357+
}
358+
333359
QTEST_MAIN(tst_QIdentityProxyModel)
334360
#include "tst_qidentityproxymodel.moc"

0 commit comments

Comments
 (0)