diff options
| author | Doris Verria <[email protected]> | 2024-02-08 22:10:09 +0100 |
|---|---|---|
| committer | Shawn Rutledge <[email protected]> | 2024-04-27 20:13:20 +0000 |
| commit | 8c44064f62b9e57dacdf1dbd8de57e07c938b9db (patch) | |
| tree | 77a5e99551bc4306074f1d765dee84b904c3c159 /src/widgets/kernel/qwidgetwindow.cpp | |
| parent | f8359084b948fd8f6027d23a869f1a0d50cc30c1 (diff) | |
QWindowPrivate: Introduce virtual setFocusToTarget
When a window gains focus, the focus will be set to the focusWidget,
if one exists, in the case of QWidgetWindow, and to the window's
contentItem's subFocusItem, in the case of QQuickWindow.
However, we want to be able to customize this as we may want to set
the focus item/widget to the first, last, prev, or next, depending
for example, on the reason the window got focus. Eg.: on a TabKey
we want to focus the next focus object, on a BackTabKey the previous
one, and so on.
To be able to do this, add a virtual method in QWindowPrivate that sets focus to the specified item, and override for QWidgetWindowPrivate.
Task-number: QTBUG-121789
Done-with: [email protected]
Change-Id: Ib5e17d6ff52c2323a4013c80bf411e92b4c8ce9b
Reviewed-by: Shawn Rutledge <[email protected]>
Diffstat (limited to 'src/widgets/kernel/qwidgetwindow.cpp')
| -rw-r--r-- | src/widgets/kernel/qwidgetwindow.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/widgets/kernel/qwidgetwindow.cpp b/src/widgets/kernel/qwidgetwindow.cpp index f51baba88b1..c5b045c8dbf 100644 --- a/src/widgets/kernel/qwidgetwindow.cpp +++ b/src/widgets/kernel/qwidgetwindow.cpp @@ -77,6 +77,39 @@ public: widget->focusWidget()->clearFocus(); } + void setFocusToTarget(QWindowPrivate::FocusTarget target) override + { + Q_Q(QWidgetWindow); + QWidget *widget = q->widget(); + if (!widget) + return; + QWidget *newFocusWidget = nullptr; + + switch (target) { + case FocusTarget::First: + newFocusWidget = q->getFocusWidget(QWidgetWindow::FirstFocusWidget); + break; + case FocusTarget::Last: + newFocusWidget = q->getFocusWidget(QWidgetWindow::LastFocusWidget); + break; + case FocusTarget::Next: { + QWidget *focusWidget = widget->focusWidget() ? widget->focusWidget() : widget; + newFocusWidget = focusWidget->nextInFocusChain() ? focusWidget->nextInFocusChain() : focusWidget; + break; + } + case FocusTarget::Prev: { + QWidget *focusWidget = widget->focusWidget() ? widget->focusWidget() : widget; + newFocusWidget = focusWidget->previousInFocusChain() ? focusWidget->previousInFocusChain() : focusWidget; + break; + } + default: + break; + } + + if (newFocusWidget) + newFocusWidget->setFocus(); + } + QRectF closestAcceptableGeometry(const QRectF &rect) const override; void processSafeAreaMarginsChanged() override |
