From 100908e400be65fea74184caaee754abc3a5afcb Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Mon, 21 Nov 2011 13:34:24 +0100 Subject: [PATCH] Add a roles argument to the dataChanged signal. This allows more granular reporting of what has changed. This change is binary incompatible and source compatible. Change-Id: I7c5beaee651a24780cc94e41383f7a80210bc603 Reviewed-by: Olivier Goffart --- src/corelib/kernel/qabstractitemmodel.cpp | 6 +- src/corelib/kernel/qabstractitemmodel.h | 3 +- .../tst_qabstractitemmodel.cpp | 61 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/corelib/kernel/qabstractitemmodel.cpp b/src/corelib/kernel/qabstractitemmodel.cpp index bda25c301e..fbeb59a553 100644 --- a/src/corelib/kernel/qabstractitemmodel.cpp +++ b/src/corelib/kernel/qabstractitemmodel.cpp @@ -1415,7 +1415,7 @@ QAbstractItemModel::~QAbstractItemModel() */ /*! - \fn void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) + \fn void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QSet &roles = QSet()) This signal is emitted whenever the data in an existing item changes. @@ -1426,6 +1426,10 @@ QAbstractItemModel::~QAbstractItemModel() When reimplementing the setData() function, this signal must be emitted explicitly. + The optional roles argument can be used to specify which data roles have actually + been modified. An empty set in the roles argument means that all roles should be + considered modified. + \sa headerDataChanged(), setData(), layoutChanged() */ diff --git a/src/corelib/kernel/qabstractitemmodel.h b/src/corelib/kernel/qabstractitemmodel.h index c7af7a284f..97c5b58482 100644 --- a/src/corelib/kernel/qabstractitemmodel.h +++ b/src/corelib/kernel/qabstractitemmodel.h @@ -45,6 +45,7 @@ #include #include #include +#include QT_BEGIN_HEADER @@ -230,7 +231,7 @@ public: #endif Q_SIGNALS: - void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); + void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QSet &roles = QSet()); void headerDataChanged(Qt::Orientation orientation, int first, int last); void layoutChanged(); void layoutAboutToBeChanged(); diff --git a/tests/auto/corelib/kernel/qabstractitemmodel/tst_qabstractitemmodel.cpp b/tests/auto/corelib/kernel/qabstractitemmodel/tst_qabstractitemmodel.cpp index 5383446f22..4b09b455ad 100644 --- a/tests/auto/corelib/kernel/qabstractitemmodel/tst_qabstractitemmodel.cpp +++ b/tests/auto/corelib/kernel/qabstractitemmodel/tst_qabstractitemmodel.cpp @@ -43,6 +43,7 @@ #include #include +#include //TESTED_CLASS=QAbstractListModel QAbstractTableModel //TESTED_FILES= @@ -109,6 +110,8 @@ private slots: void testReset(); + void testDataChanged(); + private: DynamicTreeModel *m_model; }; @@ -1780,6 +1783,64 @@ void tst_QAbstractItemModel::testReset() } +class CustomRoleModel : public QStringListModel +{ + Q_OBJECT + Q_ENUMS(Roles) +public: + enum Roles { + Custom1 = Qt::UserRole + 1, + Custom2, + UserRole + }; + + CustomRoleModel(QObject *parent = 0) + : QStringListModel(QStringList() << "a" << "b" << "c", parent) + { + + } + + void emitSignals() + { + const QModelIndex top = index(0, 0); + const QModelIndex bottom = index(2, 0); + + emit dataChanged(top, bottom); + emit dataChanged(top, bottom, QSet() << Qt::ToolTipRole); + emit dataChanged(top, bottom, QSet() << Qt::ToolTipRole << Custom1); + } +}; + +Q_DECLARE_METATYPE(QSet) + +void tst_QAbstractItemModel::testDataChanged() +{ + qRegisterMetaType >(); + + CustomRoleModel model; + + QSignalSpy withRoles(&model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QSet))); + QSignalSpy withoutRoles(&model, SIGNAL(dataChanged(QModelIndex,QModelIndex))); + + model.emitSignals(); + + QCOMPARE(withRoles.size(), withoutRoles.size()); + QCOMPARE(withRoles.size(), 3); + + const QVariantList secondEmission = withRoles.at(1); + const QVariantList thirdEmission = withRoles.at(2); + + const QSet secondRoles = secondEmission.at(2).value >(); + const QSet thirdRoles = thirdEmission.at(2).value >(); + + QCOMPARE(secondRoles.size(), 1); + QVERIFY(secondRoles.contains(Qt::ToolTipRole)); + + QCOMPARE(thirdRoles.size(), 2); + QVERIFY(thirdRoles.contains(Qt::ToolTipRole)); + QVERIFY(thirdRoles.contains(CustomRoleModel::Custom1)); +} + QTEST_MAIN(tst_QAbstractItemModel) #include "tst_qabstractitemmodel.moc"