diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index a83aecf52c..b38b482295 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -1986,6 +1986,8 @@ void QComboBox::setModel(QAbstractItemModel *model) this, SLOT(_q_emitHighlighted(QModelIndex)), Qt::UniqueConnection); } + setRootModelIndex(QModelIndex()); + bool currentReset = false; if (count()) { @@ -2024,6 +2026,8 @@ QModelIndex QComboBox::rootModelIndex() const void QComboBox::setRootModelIndex(const QModelIndex &index) { Q_D(QComboBox); + if (d->root == index) + return; d->root = QPersistentModelIndex(index); view()->setRootIndex(index); update(); diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index 556602b523..5007983aa6 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -1577,11 +1577,34 @@ void tst_QComboBox::setModel() QCOMPARE(box.currentIndex(), 0); QVERIFY(box.model() != oldModel); + // set a new root index + QModelIndex rootModelIndex; + rootModelIndex = box.model()->index(0, 0); + QVERIFY(rootModelIndex.isValid()); + box.setRootModelIndex(rootModelIndex); + QCOMPARE(box.rootModelIndex(), rootModelIndex); + + // change the model, ensure that the root index gets reset + oldModel = box.model(); + box.setModel(new QStandardItemModel(2, 1, &box)); + QCOMPARE(box.currentIndex(), 0); + QVERIFY(box.model() != oldModel); + QVERIFY(box.rootModelIndex() != rootModelIndex); + QVERIFY(box.rootModelIndex() == QModelIndex()); + // 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); + + // check that setting the very same model doesn't move the root index + rootModelIndex = box.model()->index(0, 0); + QVERIFY(rootModelIndex.isValid()); + box.setRootModelIndex(rootModelIndex); + QCOMPARE(box.rootModelIndex(), rootModelIndex); + box.setModel(box.model()); + QCOMPARE(box.rootModelIndex(), rootModelIndex); } void tst_QComboBox::setCustomModelAndView()