From f96baeb75fc36a41d2b08f880536cee5a8041e79 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Wed, 3 Dec 2014 18:08:51 +0100 Subject: QSortFilterProxyModel: honor the roles parameter of dataChanged When the source model emits dataChanged, it may tell which roles have been changed. That information was lost when using a QSortFilterProxyModel -- the proxy simply dropped that argument (meaning "all roles may have changed"). It's instead a good idea to forward the roles argument, as it may minimize hits on the proxy (on unchanged roles). [ChangeLog][QtCore][QSortFilterProxyModel] QSortFilterProxyModel now properly forwards the roles that have been changed when the source model emits dataChanged(). Task-number: QTBUG-35440 Change-Id: Ifa5213866ba04dfd57d50b5fbd47638f2191eb8e Reviewed-by: Friedemann Kleint Reviewed-by: Olivier Goffart Reviewed-by: David Faure --- src/corelib/itemmodels/qsortfilterproxymodel.cpp | 16 ++++++----- src/corelib/itemmodels/qsortfilterproxymodel.h | 2 +- .../tst_qsortfilterproxymodel.cpp | 32 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 8ed6825085..90dd984b83 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -195,7 +195,8 @@ public: } void _q_sourceDataChanged(const QModelIndex &source_top_left, - const QModelIndex &source_bottom_right); + const QModelIndex &source_bottom_right, + const QVector &roles); void _q_sourceHeaderDataChanged(Qt::Orientation orientation, int start, int end); void _q_sourceAboutToBeReset(); @@ -1131,7 +1132,8 @@ QSet QSortFilterProxyModelPrivate::handle_filter_changed( } void QSortFilterProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &source_top_left, - const QModelIndex &source_bottom_right) + const QModelIndex &source_bottom_right, + const QVector &roles) { Q_Q(QSortFilterProxyModel); if (!source_top_left.isValid() || !source_bottom_right.isValid()) @@ -1224,7 +1226,7 @@ void QSortFilterProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &sourc --source_right_column; const QModelIndex proxy_bottom_right = create_index( proxy_end_row, m->proxy_columns.at(source_right_column), it); - emit q->dataChanged(proxy_top_left, proxy_bottom_right); + emit q->dataChanged(proxy_top_left, proxy_bottom_right, roles); } } @@ -1728,8 +1730,8 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) beginResetModel(); - disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), - this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex))); + disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), + this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QVector))); disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); @@ -1781,8 +1783,8 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) QAbstractProxyModel::setSourceModel(sourceModel); - connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), - this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex))); + connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector)), + this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QVector))); connect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.h b/src/corelib/itemmodels/qsortfilterproxymodel.h index f82c5d4c20..f2debb9bdf 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.h +++ b/src/corelib/itemmodels/qsortfilterproxymodel.h @@ -165,7 +165,7 @@ private: Q_DECLARE_PRIVATE(QSortFilterProxyModel) Q_DISABLE_COPY(QSortFilterProxyModel) - Q_PRIVATE_SLOT(d_func(), void _q_sourceDataChanged(const QModelIndex &source_top_left, const QModelIndex &source_bottom_right)) + Q_PRIVATE_SLOT(d_func(), void _q_sourceDataChanged(const QModelIndex &source_top_left, const QModelIndex &source_bottom_right, const QVector &roles)) Q_PRIVATE_SLOT(d_func(), void _q_sourceHeaderDataChanged(Qt::Orientation orientation, int start, int end)) Q_PRIVATE_SLOT(d_func(), void _q_sourceAboutToBeReset()) Q_PRIVATE_SLOT(d_func(), void _q_sourceReset()) diff --git a/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp b/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp index d05ed6c20f..d5f976ed64 100644 --- a/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp +++ b/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp @@ -96,6 +96,7 @@ private slots: void changeSourceData_data(); void changeSourceData(); void changeSourceDataKeepsStableSorting_qtbug1548(); + void changeSourceDataForwardsRoles_qtbug35440(); void sortFilterRole(); void selectionFilteredOut(); void match_data(); @@ -2084,6 +2085,37 @@ void tst_QSortFilterProxyModel::changeSourceDataKeepsStableSorting_qtbug1548() checkSortedTableModel(&model, rows); } +void tst_QSortFilterProxyModel::changeSourceDataForwardsRoles_qtbug35440() +{ + QStringList strings; + for (int i = 0; i < 100; ++i) + strings << QString::number(i); + + QStringListModel model(strings); + + QSortFilterProxyModel proxy; + proxy.setSourceModel(&model); + proxy.sort(0, Qt::AscendingOrder); + + QSignalSpy spy(&proxy, &QAbstractItemModel::dataChanged); + QVERIFY(spy.isValid()); + QCOMPARE(spy.length(), 0); + + QModelIndex index; + + index = model.index(0, 0); + QVERIFY(index.isValid()); + model.setData(index, QStringLiteral("teststring"), Qt::DisplayRole); + QCOMPARE(spy.length(), 1); + QCOMPARE(spy.at(0).at(2).value >(), QVector() << Qt::DisplayRole); + + index = model.index(1, 0); + QVERIFY(index.isValid()); + model.setData(index, QStringLiteral("teststring2"), Qt::EditRole); + QCOMPARE(spy.length(), 2); + QCOMPARE(spy.at(1).at(2).value >(), QVector() << Qt::EditRole); +} + void tst_QSortFilterProxyModel::sortFilterRole() { QStandardItemModel model; -- cgit v1.2.3