From 6fd77a96b4117b7210ecd5fab1a6304cb3ebc976 Mon Sep 17 00:00:00 2001 From: Eugene Popov Date: Fri, 21 Apr 2023 15:41:13 +0300 Subject: [PATCH] FilterBar: improve keyboard behavior and tab ordering With this MR, when the filterbar has the keyboard focus: - pressing the Tab key moves the keyboard focus to the view - pressing the Down, PageDown, Up and PageUp keys moves the focus to the view and emulate pressing the same key in it (you can navigate directly from the filterbar) --- src/filterbar/filterbar.cpp | 26 +++++++++++++++++++++----- src/filterbar/filterbar.h | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/filterbar/filterbar.cpp b/src/filterbar/filterbar.cpp index 76e23d420..e24025c5b 100644 --- a/src/filterbar/filterbar.cpp +++ b/src/filterbar/filterbar.cpp @@ -10,6 +10,7 @@ #include +#include #include #include #include @@ -47,6 +48,9 @@ FilterBar::FilterBar(QWidget *parent) hLayout->addWidget(m_lockButton); hLayout->addWidget(m_filterInput); hLayout->addWidget(closeButton); + + setTabOrder(m_lockButton, closeButton); + setTabOrder(closeButton, m_filterInput); } FilterBar::~FilterBar() @@ -96,10 +100,8 @@ void FilterBar::showEvent(QShowEvent *event) } } -void FilterBar::keyReleaseEvent(QKeyEvent *event) +void FilterBar::keyPressEvent(QKeyEvent *event) { - QWidget::keyReleaseEvent(event); - switch (event->key()) { case Qt::Key_Escape: if (m_filterInput->text().isEmpty()) { @@ -107,14 +109,28 @@ void FilterBar::keyReleaseEvent(QKeyEvent *event) } else { m_filterInput->clear(); } - break; + return; case Qt::Key_Enter: case Qt::Key_Return: Q_EMIT focusViewRequest(); - break; + return; + + case Qt::Key_Down: + case Qt::Key_PageDown: + case Qt::Key_Up: + case Qt::Key_PageUp: { + Q_EMIT focusViewRequest(); + QWidget *focusWidget = QApplication::focusWidget(); + if (focusWidget && focusWidget != this) { + QApplication::sendEvent(focusWidget, event); + } + return; + } default: break; } + + QWidget::keyPressEvent(event); } diff --git a/src/filterbar/filterbar.h b/src/filterbar/filterbar.h index 8a0b81431..353055883 100644 --- a/src/filterbar/filterbar.h +++ b/src/filterbar/filterbar.h @@ -62,7 +62,7 @@ Q_SIGNALS: protected: void showEvent(QShowEvent *event) override; - void keyReleaseEvent(QKeyEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; private: QLineEdit *m_filterInput; -- 2.47.3