【问题标题】:QComboBox EventFilter to popupQComboBox EventFilter 弹出
【发布时间】:2012-04-05 06:27:17
【问题描述】:

我有一个小问题,我需要将我的事件过滤器设置为 QComboBox 弹出窗口。 我需要在按下左右键时捕捉事件。 我该怎么做?

谢谢!

【问题讨论】:

    标签: c++ linux qt events qcombobox


    【解决方案1】:

    您需要在 QComboBox 的 view() 上设置 eventFilter (http://qt-project.org/doc/qt-4.8/qcombobox.html#view)。

    【讨论】:

      【解决方案2】:

      这个问题很老了,但我提供了我的答案,因为它可以帮助其他人。

      弹出后所有事件将被发送到用于 QComboBox 弹出的列表视图。您可以使用键处理程序类监视列表视图的事件来完成任务。

      KeyPressHandler.h:

      class KeyPressHandler : public QObject
      {
          Q_OBJECT
      public:
         explicit KeyPressHandler(QObject *parent = nullptr);
         virtual ~KeyPressHandler() override;
      
      protected:
          bool eventFilter(QObject *obj, QEvent *event) override;
      };
      

      KeyPressHandler.cpp:

      #include <QCoreApplication>
      
      KeyPressHandler::KeyPressHandler(QObject *parent) : QObject(parent)
      {
      
      }
      
      KeyPressHandler::~KeyPressHandler()
      {
      }
      
      
      bool KeyPressHandler::eventFilter(QObject *obj, QEvent *event)
      {
          if (event->type() == QEvent::KeyPress)
          {
              QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
      
              switch(keyEvent->key())
              {
                case Qt::Key_Left:
                  // Send press event for the Key_Up which understood by list view
                  QCoreApplication::postEvent(obj, new QKeyEvent(QEvent::KeyPress,
                                                                 Qt::Key_Up,
                                                                 Qt::NoModifier));
                  return true;
                case Qt::Key_Right:
                  QCoreApplication::postEvent(obj, new QKeyEvent(QEvent::KeyPress,
                                                                 Qt::Key_Down,
                                                                 Qt::NoModifier));
                  return true;
                default:
                  break;
              }
          }
      
          // standard event processing
          return QObject::eventFilter(obj, event);
      }
      

      在 ComboBox 中,您需要在显示弹出窗口时安装事件过滤器。 它可以通过不同的方式完成,例如通过覆盖 QComboBox::showPopup() 函数。

      MyComboBox.h:

      #include <memory>
      #include <QComboBox>
      
      class MyComboBox : public QComboBox
      {
        Q_OBJECT
      public:
        explicit MyComboBox(QWidget *parent = 0);
      protected:
          void showPopup() override;
          void hidePopup() override;    
      private:   
          std::unique_ptr<KeyPressHandler> m_key_press_handler;
      };
      

      MyComboBox.cpp:

      ...
      void MyComboBox::showPopup()
      {
        if(!m_key_press_handler)
        {
          m_key_press_handler.reset(new KeyPressHandler());
          QAbstractItemView *v = view();
          v->installEventFilter(m_key_press_handler.get());
        }
      
        QComboBox::showPopup();
      }
      
      void MyComboBox::hidePopup()
      {
        m_key_press_handler.reset(nullptr);
        QComboBox::hidePopup();
      }
      

      【讨论】:

        【解决方案3】:

        您可能需要在代码中的某处添加以下代码。

         void MyComboBox::keyPressEvent (QKeyEvent *event)
         {
             if (event->button() ==  Qt::Key_Left) 
             {
                 // handle left key press
             } 
             if (event->button() ==  Qt::Key_Right) 
             {
                 // handle right key press
             }
         }
        

        希望这会有所帮助!

        【讨论】:

        • 不,如果我安装 eventfilter,这只会处理仅在 QCombox 上按下的键,但我需要捕获在打开的 QCombobox 弹出窗口上按下的键
        • 在你真正想要捕获按键的类中添加相同的代码怎么样?
        • 我在同一个班里加了,没有结果
        猜你喜欢
        • 1970-01-01
        • 2012-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-11
        • 1970-01-01
        相关资源
        最近更新 更多