summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2022-03-12 01:48:07 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-03-14 00:50:20 +0000
commit8bb2d655ae85c92e1ded7a1b8dfae7bc264b61ae (patch)
tree2f81bb2410c89de0c1936282fe928a3935ead3d4
parenta265c839f9ee2a6e7a764b0ad63b71675c1b892e (diff)
downloadqtbase-8bb2d655ae85c92e1ded7a1b8dfae7bc264b61ae.tar.gz
QCompleter: fix crash when setting the same model twice
Found when retesting the testcase completer.zip from QTBUG-54642 Change-Id: Id84eefeb3a33dc6d790cfa23755352381cc097a9 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 7382e5735ea734fe5e5777518394963593603c32) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/widgets/util/qcompleter.cpp2
-rw-r--r--tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp10
2 files changed, 12 insertions, 0 deletions
diff --git a/src/widgets/util/qcompleter.cpp b/src/widgets/util/qcompleter.cpp
index 85abcca0d7..727d1231aa 100644
--- a/src/widgets/util/qcompleter.cpp
+++ b/src/widgets/util/qcompleter.cpp
@@ -1120,6 +1120,8 @@ void QCompleter::setModel(QAbstractItemModel *model)
{
Q_D(QCompleter);
QAbstractItemModel *oldModel = d->proxy->sourceModel();
+ if (oldModel == model)
+ return;
#if QT_CONFIG(filesystemmodel)
if (qobject_cast<const QFileSystemModel *>(oldModel))
setCompletionRole(Qt::EditRole); // QTBUG-54642, clear FileNameRole set by QFileSystemModel
diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp
index 32437050f5..7af60ed757 100644
--- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp
+++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp
@@ -1682,6 +1682,16 @@ void tst_QComboBox::setModel()
QCOMPARE(box.rootModelIndex(), rootModelIndex);
box.setModel(box.model());
QCOMPARE(box.rootModelIndex(), rootModelIndex);
+
+ // check that setting the same model as the completer's doesn't crash
+ QCompleter *completer = new QCompleter(&box);
+ box.setEditable(true);
+ box.setCompleter(completer);
+ auto *listModel = new QStringListModel({ "one", "two" }, completer);
+ completer->setModel(listModel);
+ QCOMPARE(listModel->rowCount(), 2); // make sure it wasn't deleted
+ box.setModel(listModel);
+ QCOMPARE(listModel->rowCount(), 2); // make sure it wasn't deleted
}
void tst_QComboBox::setCustomModelAndView()