UI: Focus in menu widgets

This commit is contained in:
Nodir Temirkhodjaev 2024-02-03 10:08:04 +03:00
parent 4eda74f415
commit c90ef43a5d
5 changed files with 53 additions and 2 deletions

View File

@ -43,6 +43,7 @@ SOURCES += \
form/controls/combobox.cpp \ form/controls/combobox.cpp \
form/controls/controlutil.cpp \ form/controls/controlutil.cpp \
form/controls/doublespinbox.cpp \ form/controls/doublespinbox.cpp \
form/controls/focusablemenu.cpp \
form/controls/labelcolor.cpp \ form/controls/labelcolor.cpp \
form/controls/labeldoublespin.cpp \ form/controls/labeldoublespin.cpp \
form/controls/labelspin.cpp \ form/controls/labelspin.cpp \
@ -246,6 +247,7 @@ HEADERS += \
form/controls/combobox.h \ form/controls/combobox.h \
form/controls/controlutil.h \ form/controls/controlutil.h \
form/controls/doublespinbox.h \ form/controls/doublespinbox.h \
form/controls/focusablemenu.h \
form/controls/labelcolor.h \ form/controls/labelcolor.h \
form/controls/labeldoublespin.h \ form/controls/labeldoublespin.h \
form/controls/labelspin.h \ form/controls/labelspin.h \

View File

@ -18,7 +18,7 @@ protected:
void mouseReleaseEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override;
private: private:
bool m_pressed = false; bool m_pressed : 1 = false;
}; };
#endif // CLICKABLEMENU_H #endif // CLICKABLEMENU_H

View File

@ -13,6 +13,7 @@
#include <util/iconcache.h> #include <util/iconcache.h>
#include "combobox.h" #include "combobox.h"
#include "focusablemenu.h"
#include "labelcolor.h" #include "labelcolor.h"
#include "labeldoublespin.h" #include "labeldoublespin.h"
#include "labelspin.h" #include "labelspin.h"
@ -218,7 +219,7 @@ QMenu *ControlUtil::createMenu(QWidget *parent)
QMenu *ControlUtil::createMenuByLayout(QBoxLayout *layout, QWidget *parent) QMenu *ControlUtil::createMenuByLayout(QBoxLayout *layout, QWidget *parent)
{ {
auto menu = createMenu(parent); auto menu = new FocusableMenu(parent);
auto wa = new QWidgetAction(menu); auto wa = new QWidgetAction(menu);
auto menuWidget = new MenuWidget(menu, wa); auto menuWidget = new MenuWidget(menu, wa);

View File

@ -0,0 +1,30 @@
#include "focusablemenu.h"
#include <QEvent>
#include <QKeyEvent>
FocusableMenu::FocusableMenu(QWidget *parent) : QMenu(parent)
{
setAttribute(Qt::WA_WindowPropagation); // to inherit default font
}
bool FocusableMenu::event(QEvent *e)
{
switch (e->type()) {
case QEvent::KeyPress: {
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
return QWidget::event(e);
}
} break;
default:
break;
}
return QMenu::event(e);
}
bool FocusableMenu::focusNextPrevChild(bool next)
{
return QWidget::focusNextPrevChild(next);
}

View File

@ -0,0 +1,18 @@
#ifndef FOCUSABLEMENU_H
#define FOCUSABLEMENU_H
#include <QMenu>
class FocusableMenu : public QMenu
{
Q_OBJECT
public:
explicit FocusableMenu(QWidget *parent = nullptr);
protected:
bool event(QEvent *e) override;
bool focusNextPrevChild(bool next) override;
};
#endif // FOCUSABLEMENU_H