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 <ogoffart@woboq.com>
This commit is contained in:
Stephen Kelly 2011-11-21 13:34:24 +01:00 committed by Qt by Nokia
parent 448e1e620c
commit 100908e400
3 changed files with 68 additions and 2 deletions

View File

@ -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<int> &roles = QSet<int>())
This signal is emitted whenever the data in an existing item changes. 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 When reimplementing the setData() function, this signal must be emitted
explicitly. 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() \sa headerDataChanged(), setData(), layoutChanged()
*/ */

View File

@ -45,6 +45,7 @@
#include <QtCore/qvariant.h> #include <QtCore/qvariant.h>
#include <QtCore/qobject.h> #include <QtCore/qobject.h>
#include <QtCore/qhash.h> #include <QtCore/qhash.h>
#include <QtCore/qset.h>
QT_BEGIN_HEADER QT_BEGIN_HEADER
@ -230,7 +231,7 @@ public:
#endif #endif
Q_SIGNALS: Q_SIGNALS:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QSet<int> &roles = QSet<int>());
void headerDataChanged(Qt::Orientation orientation, int first, int last); void headerDataChanged(Qt::Orientation orientation, int first, int last);
void layoutChanged(); void layoutChanged();
void layoutAboutToBeChanged(); void layoutAboutToBeChanged();

View File

@ -43,6 +43,7 @@
#include <QtCore/QtCore> #include <QtCore/QtCore>
#include <QtWidgets/QSortFilterProxyModel> #include <QtWidgets/QSortFilterProxyModel>
#include <QtWidgets/QStringListModel>
//TESTED_CLASS=QAbstractListModel QAbstractTableModel //TESTED_CLASS=QAbstractListModel QAbstractTableModel
//TESTED_FILES= //TESTED_FILES=
@ -109,6 +110,8 @@ private slots:
void testReset(); void testReset();
void testDataChanged();
private: private:
DynamicTreeModel *m_model; 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<int>() << Qt::ToolTipRole);
emit dataChanged(top, bottom, QSet<int>() << Qt::ToolTipRole << Custom1);
}
};
Q_DECLARE_METATYPE(QSet<int>)
void tst_QAbstractItemModel::testDataChanged()
{
qRegisterMetaType<QSet<int> >();
CustomRoleModel model;
QSignalSpy withRoles(&model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QSet<int>)));
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<int> secondRoles = secondEmission.at(2).value<QSet<int> >();
const QSet<int> thirdRoles = thirdEmission.at(2).value<QSet<int> >();
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) QTEST_MAIN(tst_QAbstractItemModel)
#include "tst_qabstractitemmodel.moc" #include "tst_qabstractitemmodel.moc"