Fix QStringListModel::setData to check for actual changes
QStringListModel::setData documentation states that "The dataChanged() signal is emitted if the item is changed." This patch actually respects the doc. setData will check that the data actually changed before sending the dataChanged signal. [ChangeLog][QtCore][QStringListModel] setData will now emit the dataChanged() signal only if the string set is different from the one already contained in the model Change-Id: I4308a6f3b4851203fb899c5e29a36076e0c32f2f Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
parent
3306b16239
commit
ff835a5030
@ -184,7 +184,10 @@ bool QStringListModel::setData(const QModelIndex &index, const QVariant &value,
|
||||
{
|
||||
if (index.row() >= 0 && index.row() < lst.size()
|
||||
&& (role == Qt::EditRole || role == Qt::DisplayRole)) {
|
||||
lst.replace(index.row(), value.toString());
|
||||
const QString valueString = value.toString();
|
||||
if (lst.at(index.row()) == valueString)
|
||||
return true;
|
||||
lst.replace(index.row(), valueString);
|
||||
QVector<int> roles;
|
||||
roles.reserve(2);
|
||||
roles.append(Qt::DisplayRole);
|
||||
|
@ -81,6 +81,8 @@ private slots:
|
||||
void setData_emits_both_roles_data();
|
||||
void setData_emits_both_roles();
|
||||
|
||||
void setData_emits_on_change_only();
|
||||
|
||||
void supportedDragDropActions();
|
||||
};
|
||||
|
||||
@ -246,6 +248,24 @@ void tst_QStringListModel::setData_emits_both_roles()
|
||||
expected);
|
||||
}
|
||||
|
||||
void tst_QStringListModel::setData_emits_on_change_only()
|
||||
{
|
||||
QStringListModel model(QStringList{QStringLiteral("one"), QStringLiteral("two")});
|
||||
QSignalSpy dataChangedSpy(&model, &QAbstractItemModel::dataChanged);
|
||||
QVERIFY(dataChangedSpy.isValid());
|
||||
const QModelIndex modelIdx = model.index(0, 0);
|
||||
const QString newStringData = QStringLiteral("test");
|
||||
QVERIFY(model.setData(modelIdx, newStringData));
|
||||
QCOMPARE(dataChangedSpy.count(), 1);
|
||||
const QList<QVariant> spyList = dataChangedSpy.takeFirst();
|
||||
QCOMPARE(spyList.at(0).value<QModelIndex>(), modelIdx);
|
||||
QCOMPARE(spyList.at(1).value<QModelIndex>(), modelIdx);
|
||||
const QVector<int> expectedRoles{Qt::DisplayRole, Qt::EditRole};
|
||||
QCOMPARE(spyList.at(2).value<QVector<int> >(), expectedRoles);
|
||||
QVERIFY(model.setData(modelIdx, newStringData));
|
||||
QVERIFY(dataChangedSpy.isEmpty());
|
||||
}
|
||||
|
||||
void tst_QStringListModel::supportedDragDropActions()
|
||||
{
|
||||
QStringListModel model;
|
||||
|
Loading…
Reference in New Issue
Block a user