diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2019-12-29 13:24:34 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2020-01-28 06:24:26 +0100 |
commit | b0c804f345a383d2813f4709632baa43a4275646 (patch) | |
tree | 5b522423ee7cb3475e26112950ed00086a0ef6e7 | |
parent | QButtonGroup: deprecate overloaded signals (diff) | |
download | qtbase-b0c804f345a383d2813f4709632baa43a4275646.tar.xz qtbase-b0c804f345a383d2813f4709632baa43a4275646.zip |
QComboBox: unify behavior of setModel() and modelReset
When a new model is set, a valid index is selected. When a model is
reset, this is not the case which is slightly inconsistent. Fix it by
using the same logic to find a valid index when the model is reset
Fixes: QTBUG-80998
Change-Id: I6c167511e199a6664343cf1dc3bcd27c65389bfd
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
-rw-r--r-- | src/widgets/widgets/qcombobox.cpp | 37 | ||||
-rw-r--r-- | src/widgets/widgets/qcombobox_p.h | 1 | ||||
-rw-r--r-- | tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp | 11 |
3 files changed, 26 insertions, 23 deletions
diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index aa3788c404..95c2bea3fe 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -293,8 +293,7 @@ void QComboBoxPrivate::_q_modelReset() lineEdit->setText(QString()); updateLineEditGeometry(); } - if (currentIndex.row() != indexBeforeChange) - _q_emitCurrentIndexChanged(currentIndex); + trySetValidIndex(); modelChanged(); q->update(); } @@ -304,6 +303,25 @@ void QComboBoxPrivate::_q_modelDestroyed() model = QAbstractItemModelPrivate::staticEmptyModel(); } +void QComboBoxPrivate::trySetValidIndex() +{ + Q_Q(QComboBox); + bool currentReset = false; + + const int rowCount = q->count(); + for (int pos = 0; pos < rowCount; ++pos) { + const QModelIndex idx(model->index(pos, modelColumn, root)); + if (idx.flags() & Qt::ItemIsEnabled) { + setCurrentIndex(idx); + currentReset = true; + break; + } + } + + if (!currentReset) + setCurrentIndex(QModelIndex()); +} + QRect QComboBoxPrivate::popupGeometry(int screen) const { return QStylePrivate::useFullScreenForPopup() @@ -2200,20 +2218,7 @@ void QComboBox::setModel(QAbstractItemModel *model) setRootModelIndex(QModelIndex()); - bool currentReset = false; - - const int rowCount = count(); - for (int pos=0; pos < rowCount; pos++) { - if (d->model->index(pos, d->modelColumn, d->root).flags() & Qt::ItemIsEnabled) { - setCurrentIndex(pos); - currentReset = true; - break; - } - } - - if (!currentReset) - setCurrentIndex(-1); - + d->trySetValidIndex(); d->modelChanged(); } diff --git a/src/widgets/widgets/qcombobox_p.h b/src/widgets/widgets/qcombobox_p.h index 7a3fcf6e0f..3e78e756a6 100644 --- a/src/widgets/widgets/qcombobox_p.h +++ b/src/widgets/widgets/qcombobox_p.h @@ -371,6 +371,7 @@ public: void _q_rowsRemoved(const QModelIndex &parent, int start, int end); void updateArrow(QStyle::StateFlag state); bool updateHoverControl(const QPoint &pos); + void trySetValidIndex(); QRect popupGeometry(int screen = -1) const; QStyle::SubControl newHoverControl(const QPoint &pos); int computeWidthHint() const; diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index ea1b330152..3878e7ccb2 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -2756,10 +2756,7 @@ void tst_QComboBox::resetModel() class StringListModel : public QStringListModel { public: - StringListModel(const QStringList &list) : QStringListModel(list) - { - } - + using QStringListModel::QStringListModel; void reset() { QStringListModel::beginResetModel(); @@ -2767,8 +2764,8 @@ void tst_QComboBox::resetModel() } }; QComboBox cb; - StringListModel model( QStringList() << "1" << "2"); - QSignalSpy spy(&cb, SIGNAL(currentIndexChanged(int))); + StringListModel model({"1", "2"}); + QSignalSpy spy(&cb, QOverload<int>::of(&QComboBox::currentIndexChanged)); QCOMPARE(spy.count(), 0); QCOMPARE(cb.currentIndex(), -1); //no selection @@ -2779,7 +2776,7 @@ void tst_QComboBox::resetModel() model.reset(); QCOMPARE(spy.count(), 2); - QCOMPARE(cb.currentIndex(), -1); //no selection + QCOMPARE(cb.currentIndex(), 0); //first item selected } |