简体   繁体   English

QComboBox EventFilter 弹出

[英]QComboBox EventFilter to popup

I have a little problem, I need to set my event filter to QComboBox popup.我有一个小问题,我需要将我的事件过滤器设置为 QComboBox 弹出窗口。 I need to catch events when left and right keys are pressed.我需要在按下左右键时捕捉事件。 How can I do this?我怎样才能做到这一点?

Thank you!谢谢!

You need to set the eventFilter on QComboBox's view() (http://qt-project.org/doc/qt-4.8/qcombobox.html#view).您需要在 QComboBox 的 view() (http://qt-project.org/doc/qt-4.8/qcombobox.html#view) 上设置 eventFilter。

The question is quite old, but I provide my answer since it can help someone else.这个问题很老了,但我提供了我的答案,因为它可以帮助别人。

After popup all events will be sent to the list view used for the QComboBox popup.弹出后,所有事件都将发送到用于 QComboBox 弹出窗口的列表视图。 You can get the things done using key handler class watching on events for list view.您可以使用密钥处理程序 class 监视列表视图的事件来完成这些事情。

KeyPressHandler.h: 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:按键处理程序.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);
}

In ComboBox you will need to install event filter when popup is shown.在 ComboBox 中,您需要在显示弹出窗口时安装事件过滤器。 It can be done in different ways, for example by overriding QComboBox::showPopup() function.它可以通过不同的方式完成,例如通过覆盖QComboBox::showPopup() function。

MyComboBox.h:我的组合框.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:我的组合框.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();
}

You may need to add following code somewhere in your code.您可能需要在代码中的某处添加以下代码。

 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
     }
 }

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM