QIdentityProxyModel: avoid emitting reset in no-op setSourceModel

QSortFilterProxyModel does this exactly, it was missing in
QIdentityProxyModel.

Change-Id: If14bc7a377c1ad9235dd70222e3bfbf88eef11c0
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
(cherry picked from commit 5e971101f016e6e7d015594c539612e770e2999d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
David Faure 2025-06-10 15:23:15 +02:00 committed by Qt Cherry-pick Bot
parent db28f1912c
commit 4d644342ad
2 changed files with 16 additions and 4 deletions

View File

@ -291,10 +291,13 @@ QVariant QIdentityProxyModel::headerData(int section, Qt::Orientation orientatio
*/ */
void QIdentityProxyModel::setSourceModel(QAbstractItemModel* newSourceModel) void QIdentityProxyModel::setSourceModel(QAbstractItemModel* newSourceModel)
{ {
beginResetModel();
Q_D(QIdentityProxyModel); Q_D(QIdentityProxyModel);
if (newSourceModel == d->model)
return;
beginResetModel();
// Call QObject::disconnect() unconditionally, if there is an existing source // Call QObject::disconnect() unconditionally, if there is an existing source
// model, it's disconnected, and if there isn't, then calling disconnect() on // model, it's disconnected, and if there isn't, then calling disconnect() on
// a default-constructed Connection does nothing // a default-constructed Connection does nothing

View File

@ -357,10 +357,19 @@ void tst_QIdentityProxyModel::reset()
resetCommand.doCommand(); resetCommand.doCommand();
} }
QVERIFY(modelBeforeSpy.size() == 1 && 1 == proxyBeforeSpy.size()); QCOMPARE(modelBeforeSpy.size(), 1);
QVERIFY(modelAfterSpy.size() == 1 && 1 == proxyAfterSpy.size()); QCOMPARE(modelAfterSpy.size(), 1);
QCOMPARE(proxyBeforeSpy.size(), 1);
QCOMPARE(proxyAfterSpy.size(), 1);
verifyIdentity(&model); verifyIdentity(&model);
// setSourceModel again shouldn't emit reset
m_proxy->setSourceModel(&model);
QCOMPARE(proxyBeforeSpy.size(), 1);
QCOMPARE(proxyAfterSpy.size(), 1);
m_proxy->setSourceModel(0); m_proxy->setSourceModel(0);
} }