How do you suppress a Qt main menu keyboard shortcut?

746 views Asked by At

For example, consider a main menu item that has the Delete key as a shortcut (with Qt::WindowShortcut as context). I want another QWidget to handle the Delete key when focused. This is not possible because the Delete key is processed by the main menu. I've tried grabbing the keyboard on QWidget focus but that doesn't do anything. Is this event possible?

1

There are 1 answers

0
Prasad Silva On BEST ANSWER

I was able to get the behavior I wanted by installing an event filter on qApp when the QWidget is focused (remove it when losing focus), and returning true for all QEvent::Shortcut types.

void    MyWidget::focusInEvent( QFocusEvent *event )
{
    qApp->installEventFilter(this);
}

void    MyWidget::focusOutEvent( QFocusEvent *event )
{
    qApp->removeEventFilter(this);
}

bool    MyWidget::eventFilter( QObject *target, QEvent *event )
{
    if (event->type() == QEvent::Shortcut)
    {
        // If I care about this shortcut, then return true to intercept
        // Else, return false to let the application process it
    }

    return false;
}

If there's a better way, I'd love to hear it!