summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qcombobox.cpp
diff options
context:
space:
mode:
authorAxel Spoerl <axel.spoerl@qt.io>2023-03-22 14:34:40 +0100
committerAxel Spoerl <axel.spoerl@qt.io>2023-03-28 14:45:24 +0100
commitc4b62ee501741113c0acea6c5f404d15c23fafc3 (patch)
tree3c7497730e9e7b487835bdbc6791a7c4f1125942 /src/widgets/widgets/qcombobox.cpp
parentc4f59f96b91c266aa2aea3c80a5c7e3d1f417e4a (diff)
QComboBox: reset indexBeforeChange to -1 if index is invalidated
The member variable indexBeforeChange is member-initialized with -1 and changed to the current row, if a valid index is set. It is used to check, if the model has been reset to an empty model and/or the index has been invalidated. The result is used to decide, if the currentIndexChanged signal is emitted or not. If a combo box had a valid index and it is invalidated afterwards (e.g. because the combobox has no more entries), indexBeforeChange is not reset to -1. The redundant signal emission is therefore prevented only the first time. This patch resets indexBeforeChange if the index is invalidated or the last item is removed from the combo box. It also adds a no-op check to tst_QComboBox::currentIndex. The test data sets "check that setting the index to -1 works" and "check that current index is invalid when removing the only item" check the respective use cases. As a drive-by, variable names and QObect::connect syntax have been cleaned up in tst_QComboBox::currentIndex. Fixes: QTBUG-108614 Pick-to: 6.5 6.2 Change-Id: Ib6dfa887d9247f2c47df065039d69ba57c32fa24 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/widgets/qcombobox.cpp')
-rw-r--r--src/widgets/widgets/qcombobox.cpp15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp
index 7bfce05d68..80206b24ac 100644
--- a/src/widgets/widgets/qcombobox.cpp
+++ b/src/widgets/widgets/qcombobox.cpp
@@ -1121,6 +1121,12 @@ void QComboBoxPrivate::_q_rowsRemoved(const QModelIndex &parent, int /*start*/,
q->updateGeometry();
}
+ // model has removed the last row
+ if (model->rowCount(root) == 0) {
+ setCurrentIndex(QModelIndex());
+ return;
+ }
+
// model has changed the currentIndex
if (currentIndex.row() != indexBeforeChange) {
if (!currentIndex.isValid() && q->count()) {
@@ -2127,10 +2133,15 @@ void QComboBoxPrivate::setCurrentIndex(const QModelIndex &mi)
}
updateLineEditGeometry();
}
- // If the model was reset to an empty, currentIndex will be invalidated
+ // If the model was reset to an empty one, 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.
+ // be advertised. So an explicit check for this condition is needed.
+ // The variable used for that check has to be reset when a previously valid
+ // index becomes invalid.
const bool modelResetToEmpty = !normalized.isValid() && indexBeforeChange != -1;
+ if (modelResetToEmpty)
+ indexBeforeChange = -1;
+
if (indexChanged || modelResetToEmpty) {
q->update();
_q_emitCurrentIndexChanged(currentIndex);