0

I have a little problem, I need to set my event filter to QComboBox popup. I need to catch events when left and right keys are pressed. How can I do this?

Thank you!

evghin
  • 47
  • 5

3 Answers3

2

You need to set the eventFilter on QComboBox's view() (http://qt-project.org/doc/qt-4.8/qcombobox.html#view).

MrFox
  • 1,244
  • 1
  • 11
  • 24
0

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!

Hiren
  • 341
  • 1
  • 4
  • 17
  • 1
    No, this handles just keys which pressed only on QCombox this equally if I install eventfilter, but I need to catch keys which pressed on opened QCombobox popup – evghin Apr 05 '12 at 07:39
  • How about adding the same code in the class where you actually wants to capture key presses? – Hiren Apr 05 '12 at 07:51
  • I added in the same class, no results – evghin Apr 05 '12 at 07:57
0

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. You can get the things done using key handler class watching on events for list view.

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

In ComboBox you will need to install event filter when popup is shown. It can be done in different ways, for example by overriding QComboBox::showPopup() function.

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();
}
mlytvyn
  • 16
  • 3