Skip to content

Commit

Permalink
QCompleter: fix crash when setting the same model twice
Browse files Browse the repository at this point in the history
Found when retesting the testcase completer.zip from QTBUG-54642

Pick-to: 6.3 6.2 5.15
Change-Id: Id84eefeb3a33dc6d790cfa23755352381cc097a9
Reviewed-by: Volker Hilsheimer <[email protected]>
  • Loading branch information
dfaure-kdab committed Mar 13, 2022
1 parent 2140eda commit 7382e57
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/widgets/util/qcompleter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,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
Expand Down
10 changes: 10 additions & 0 deletions tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,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()
Expand Down

0 comments on commit 7382e57

Please sign in to comment.