diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index 2da6183a1e0..9caac239f96 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -2135,7 +2135,11 @@ void QComboBoxPrivate::setCurrentIndex(const QModelIndex &mi) } updateLineEditGeometry(); } - if (indexChanged) { + // If the model was reset to an empty, currentIndex will be invalidated + // (because it's a QPersistentModelIndex), but the index change will never + // be advertised. So we need an explicit check for such condition. + const bool modelResetToEmpty = !normalized.isValid() && indexBeforeChange != -1; + if (indexChanged || modelResetToEmpty) { q->update(); _q_emitCurrentIndexChanged(currentIndex); } diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index 987db3e3998..4ca3a1b5a5f 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -146,6 +146,7 @@ private slots: void checkEmbeddedLineEditWhenStyleSheetIsSet(); void propagateStyleChanges(); void buttonPressKeys(); + void clearModel(); private: PlatformInputContext m_platformInputContext; @@ -3593,5 +3594,37 @@ void tst_QComboBox::buttonPressKeys() } } +void tst_QComboBox::clearModel() +{ + using namespace Qt::StringLiterals; + QStringListModel model({ "one"_L1, "two"_L1, "three"_L1 }); + + QComboBox combo; + combo.setModel(&model); + combo.setCurrentIndex(1); + + QCOMPARE(combo.currentIndex(), 1); + QCOMPARE(combo.currentText(), model.index(1).data().toString()); + + QSignalSpy indexSpy(&combo, &QComboBox::currentIndexChanged); + QSignalSpy textSpy(&combo, &QComboBox::currentTextChanged); + + QVERIFY(indexSpy.isEmpty()); + QVERIFY(textSpy.isEmpty()); + + model.setStringList({}); + + QCOMPARE(indexSpy.count(), 1); + const int index = indexSpy.takeFirst().at(0).toInt(); + QCOMPARE(index, -1); + + QCOMPARE(textSpy.count(), 1); + const QString text = textSpy.takeFirst().at(0).toString(); + QCOMPARE(text, QString()); + + QCOMPARE(combo.currentIndex(), -1); + QCOMPARE(combo.currentText(), QString()); +} + QTEST_MAIN(tst_QComboBox) #include "tst_qcombobox.moc"