summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews/qabstractitemview.cpp
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-03-20 16:44:26 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-03-24 00:42:00 +0100
commitdfb4697e4a4828acd47292a89207b3975ec6766e (patch)
treec3f64e87c000d049f573036f1deec0205c2d1bbb /src/widgets/itemviews/qabstractitemview.cpp
parent6880b7c39b2c7474be1c9adcabbe0b7d6596822f (diff)
QAbstractItemView: with single selection, deselect on Ctrl+Release
After cbf1b4bc60bca3994b8f8685ee922e53a6b4eed2 the selected item got deselected on Ctrl+Press, which made Ctrl+dragging a selected item impossible. Only deselect on Ctrl+Release. Add scenario to existing test case, and update the documentation to clarify the properties involved, and to point out that the event parameter might be nullptr. Fixes: QTBUG-101647 Pick-to: 6.3 6.2 Change-Id: I749b1cb1a0a311f5c1d4c333984716f05f2c90b5 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io> Reviewed-by: Volker Enderlein <volker.enderlein@ifm-chemnitz.de> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/itemviews/qabstractitemview.cpp')
-rw-r--r--src/widgets/itemviews/qabstractitemview.cpp31
1 files changed, 22 insertions, 9 deletions
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index 16b5c26096..a4dee5e48c 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.cpp
@@ -4086,9 +4086,10 @@ void QAbstractItemView::doAutoScroll()
}
/*!
- Returns the SelectionFlags to be used when updating a selection with
- to include the \a index specified. The \a event is a user input event,
- such as a mouse or keyboard event.
+ Returns the SelectionFlags to be used when updating a selection model
+ for the specified \a index. The result depends on the current
+ selectionMode(), and on the user input event \a event, which can be
+ \nullptr.
Reimplement this function to define your own selection behavior.
@@ -4105,12 +4106,24 @@ QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QM
case NoSelection: // Never update selection model
return QItemSelectionModel::NoUpdate;
case SingleSelection: // ClearAndSelect on valid index otherwise NoUpdate
- if (event && event->type() == QEvent::MouseButtonRelease)
- return QItemSelectionModel::NoUpdate;
- if ((keyModifiers & Qt::ControlModifier) && d->selectionModel->isSelected(index) && event->type() != QEvent::MouseMove)
- return QItemSelectionModel::Deselect | d->selectionBehaviorFlags();
- else
- return QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
+ if (event) {
+ switch (event->type()) {
+ case QEvent::MouseButtonPress:
+ // press with any modifiers on a selected item does nothing
+ if (d->pressedAlreadySelected)
+ return QItemSelectionModel::NoUpdate;
+ break;
+ case QEvent::KeyPress:
+ case QEvent::MouseButtonRelease:
+ // ctrl-release on selected item deselects
+ if ((keyModifiers & Qt::ControlModifier) && d->selectionModel->isSelected(index))
+ return QItemSelectionModel::Deselect | d->selectionBehaviorFlags();
+ break;
+ default:
+ break;
+ }
+ }
+ return QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
case MultiSelection:
return d->multiSelectionCommand(index, event);
case ExtendedSelection: