浏览代码

fix ButtonPopup

Le Tan 4 天之前
父节点
当前提交
72a7512356
共有 3 个文件被更改,包括 16 次插入7 次删除
  1. 12 0
      src/widgets/buttonpopup.cpp
  2. 2 0
      src/widgets/buttonpopup.h
  3. 2 7
      src/widgets/tagviewer.cpp

+ 12 - 0
src/widgets/buttonpopup.cpp

@@ -1,6 +1,7 @@
 #include "buttonpopup.h"
 
 #include <QWidgetAction>
+#include <QKeyEvent>
 
 #include <utils/widgetutils.h>
 
@@ -17,6 +18,17 @@ ButtonPopup::ButtonPopup(QToolButton *p_btn, QWidget *p_parent)
 #endif
 }
 
+void ButtonPopup::keyPressEvent(QKeyEvent *p_event)
+{
+    const int key = p_event->key();
+    if (key == Qt::Key_Return || key == Qt::Key_Enter) {
+        // Swallow Enter/Return key here to avoid hiding the popup.
+        p_event->accept();
+        return;
+    }
+    QMenu::keyPressEvent(p_event);
+}
+
 void ButtonPopup::addWidget(QWidget *p_widget)
 {
     auto act = new QWidgetAction(this);

+ 2 - 0
src/widgets/buttonpopup.h

@@ -15,6 +15,8 @@ namespace vnotex
         ButtonPopup(QToolButton *p_btn, QWidget *p_parent = nullptr);
 
     protected:
+        void keyPressEvent(QKeyEvent *p_event) override;
+
         void addWidget(QWidget *p_widget);
 
         // Button for this menu.

+ 2 - 7
src/widgets/tagviewer.cpp

@@ -48,6 +48,8 @@ void TagViewer::setupUI()
     m_searchLineEdit->setToolTip(tr("[Shift+Enter] to add current selected tag in the list"));
     connect(m_searchLineEdit, &QLineEdit::textChanged,
             this, &TagViewer::searchAndFilter);
+    connect(m_searchLineEdit, &QLineEdit::returnPressed,
+            this, &TagViewer::handleSearchLineEditReturnPressed);
     mainLayout->addWidget(m_searchLineEdit);
 
     auto tagNameValidator = new QRegularExpressionValidator(QRegularExpression("[^>]*"), m_searchLineEdit);
@@ -88,13 +90,6 @@ bool TagViewer::eventFilter(QObject *p_obj, QEvent *p_event)
                 m_searchLineEdit->setFocus();
             }
             return true;
-        } else if (key == Qt::Key_Return || key == Qt::Key_Enter) {
-            if (p_obj == m_searchLineEdit) {
-                // Pressing twice will make the popup hide if we use the signal returnPressed,
-                // so we handle it here earlier.
-                handleSearchLineEditReturnPressed();
-                return true;
-            }
         }
     }