[system/dolphin] src/itemactions: SetFolderIconItemAction: focus the last button on Shift+Tab from below

Méven Car <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 548b58110e14ab1639be0daa39254bae83d46a04 by Méven Car.
Committed on 06/08/2026 at 09:35.
Pushed by meven into branch 'master'.

SetFolderIconItemAction: focus the last button on Shift+Tab from below

The button strip focused its first button unconditionally via a focus proxy, so
Shift+Tab into it from below landed on the first button. Qt 6.11 focuses a
QWidgetAction with a FocusIn reason that reflects the direction, so focus the
last button when entered from below and the first from above. Older Qt always
reports Tab and keeps the old behavior.

M  +16   -9    src/itemactions/setfoldericonitemaction.cpp

https://invent.kde.org/system/dolphin/-/commit/548b58110e14ab1639be0daa39254bae83d46a04

diff --git a/src/itemactions/setfoldericonitemaction.cpp b/src/itemactions/setfoldericonitemaction.cpp
index a90864d868..329c355045 100644
--- a/src/itemactions/setfoldericonitemaction.cpp
+++ b/src/itemactions/setfoldericonitemaction.cpp
@@ -92,6 +92,21 @@ public:
 
     bool eventFilter(QObject *object, QEvent *event) override
     {
+        if (event->type() == QEvent::FocusIn && !qobject_cast<QPushButton *>(object)) {
+            // The menu focuses the container widget; the focus reason gives the direction,
+            // so enter on the last button when approached from below and the first from above.
+            const auto buttons = static_cast<QWidget *>(object)->findChildren<QPushButton *>();
+            if (!buttons.isEmpty()) {
+                const Qt::FocusReason reason = static_cast<QFocusEvent *>(event)->reason();
+                if (reason == Qt::BacktabFocusReason) {
+                    buttons.last()->setFocus(reason);
+                } else {
+                    buttons.first()->setFocus(reason);
+                }
+                return true;
+            }
+        }
+
         if (event->type() == QEvent::KeyPress) {
             const QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
             auto widget = qobject_cast<QWidget *>(object);
@@ -122,9 +137,6 @@ public:
             }
         }
 
-        // TODO implement proper SHIFT+TAB
-        // See https://bugreports.qt.io/browse/QTBUG-137298
-
         return false;
     }
 
@@ -133,7 +145,6 @@ public:
         QWidget *widget = new QWidget(parent);
         auto layout = new QHBoxLayout(widget);
 
-        bool firstAction = false;
         for (const auto action : std::as_const(m_actions)) {
             if (!action->parent()) {
                 action->setParent(widget);
@@ -151,11 +162,6 @@ public:
             connect(action, &QAction::toggled, p, &QPushButton::setChecked);
 
             layout->addWidget(p);
-
-            if (!firstAction) {
-                widget->setFocusProxy(p);
-                firstAction = true;
-            }
         }
 
         auto p = new QPushButton(widget);
@@ -166,6 +172,7 @@ public:
         p->installEventFilter(this);
 
         widget->setFocusPolicy(Qt::StrongFocus);
+        widget->installEventFilter(this);
 
         return widget;
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.