Intercepting Tab key press to manage focus switching manually
Asked Answered
W

4

10

I want to intercept Tab key press in my main window to prevent Qt from switching focus. Here's what I've tried so far:

bool CMainWindow::event(QEvent * e)
{
    if (e && e->type() == QEvent::KeyPress)
    {
        QKeyEvent * keyEvent = dynamic_cast<QKeyEvent*>(e);
        if (keyEvent && keyEvent->key() == Qt::Key_Tab)
            return true;
    }
    return QMainWindow::event(e);
}

This doesn't work, event isn't called when I press Tab. How to achieve what I want?

Woolly answered 10/8, 2013 at 8:20 Comment(0)
W
4

Reimplementing virtual bool QApplication::notify(QObject * receiver, QEvent * e) and pasting the code from my question there works.

Woolly answered 10/8, 2013 at 12:42 Comment(0)
G
13

The most elegant way I found to avoid focus change is to reimplement in your class derived from QWidget the method bool focusNextPrevChild(bool next) and simply return FALSE. In case you want to allow it, return TRUE.

Like other keys you get now also the key Qt::Key_Tab in keyPressEvent(QKeyEvent* event)

Gradation answered 25/1, 2014 at 14:15 Comment(0)
W
4

Reimplementing virtual bool QApplication::notify(QObject * receiver, QEvent * e) and pasting the code from my question there works.

Woolly answered 10/8, 2013 at 12:42 Comment(0)
R
3

You can achieve by using setFocusPolicy( Qt::NoFocus) property of QWidget. You can set Focus policy on widget which doesn't require tab focus. I think the reason why event handler is not calling, because Tab is managed by Qt framework internally. Please see QWidget::setTabOrder API, which is static.

Resor answered 13/8, 2013 at 16:52 Comment(1)
I don't need to fully disable focus switch, just want to do it my way. Tried focus policy - that wasn't enough. But I didn't know about QWidget::setTabOrder, thanks.Woolly
K
-1

You'll need to install an event filter on your main window in order to receive the events. You can use installEventFilter method for this. Another option is to override the keyPressEvent method to handle the key presses.

Konstanz answered 10/8, 2013 at 11:21 Comment(8)
keyPressEvent isn't being called either for Tab key. And neither does eventFilter.Woolly
Can you add the code where how you installed the event filter / overridden the keyPressEvent method?Konstanz
It's exactly the same, just different method names. And I have checked that my overriden methods are being called when I press keys other than Tab.Woolly
For the keyPressEvent way... The signature of the overridden method is exactly "void CMainWindow::keyPressEvent(QKeyEvent* event)"?Konstanz
For the event way... Did you add the installEventFilter(this) line to the constructor of your CMainWindow class?Konstanz
Of course. Like I said, both methods are being called for other, "normal" keys. How could they if I made a mistake in the method's signature. It won't take you longer than 5 minutes to create a test app and confirm neither works for Tab.Woolly
Found a solution, check my answer if interested.Woolly
Sorry, I misread "event isn't called when I press Tab" as function not called at all... my bad. It is actually not working for me as well. I can catch the event when I'm changing from Qt::KeyPress to Qt::KeyRelease, but this is obviously too late for you. Maybe you want to try to override the focusOutEvent (or similar methods) and simply do nothing there?Konstanz

© 2022 - 2024 — McMap. All rights reserved.