From 95092e20113c3c69332b67a50e0c104cebb96932 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 18 Dec 2014 16:07:27 +0100 Subject: [PATCH] Make setting twice the very same model on QComboBox a no-op Basically, introduce a small check for this corner-case situation. Setting a model doesn't just store a pointer, but also resets the current index, changes the line edit completion model, and so on, and seems silly to trigger changes in such cases. [ChangeLog][QtWidgets][QComboBox] A QComboBox does not reset itself any more when setting the same model more than once. Change-Id: If028b36cdfaa5552c156dd900e123ca9a04d4e3d Reviewed-by: Olivier Goffart --- src/widgets/widgets/qcombobox.cpp | 3 +++ tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index 40cf2f0f95..a83aecf52c 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -1931,6 +1931,9 @@ void QComboBox::setModel(QAbstractItemModel *model) return; } + if (model == d->model) + return; + #ifndef QT_NO_COMPLETER if (d->lineEdit && d->lineEdit->completer() && d->lineEdit->completer() == d->completer) diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index ac32ee4968..556602b523 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -1576,6 +1576,12 @@ void tst_QComboBox::setModel() box.setModel(new QStandardItemModel(2,1, &box)); QCOMPARE(box.currentIndex(), 0); QVERIFY(box.model() != oldModel); + + // check that setting the very same model doesn't move the current item + box.setCurrentIndex(1); + QCOMPARE(box.currentIndex(), 1); + box.setModel(box.model()); + QCOMPARE(box.currentIndex(), 1); } void tst_QComboBox::setCustomModelAndView()