How do I disable Qt's behavior on Linux of capturing arrow keys for widget focus navigation?

  • I'm working on a Qt app that is developed primarily on MacOS, but that is then built and tested on Linux as well.

    We define a keyPressEvent() method in our main window class to respond to certain keyboard events. Among others, we respond to Qt::Key_Left, Qt::Key_Right and Qt::Key_Space. This works great on MacOS. On Linux, however, we never get these events.

    Doing some Googling (and confirming it with our app's behavior on Linux), it seems that the reason for this is that Qt uses these keys for keyboard navigation of button widgets in the application's GUI. If I press the arrow keys on Linux, I cycle through all the active button widgets, selecting each on in turn. If I click the spacebar, the currently selected button is pressed.

    All I've been able to find so far by Googling is suggestions as to how to either subclass or apply filters to specific buttons to avoid this behavior by having the button ignore the event and pass it along. But I don'w want to have to do this for every button widget I ever put into my GUI. That's just lame.

    Is there a way to disable this behavior globally and allow my app code to actually get ALL of the arrow key and spacebar events?

  • You could, obviously, use the same base class for all your buttons. But I guess you want to be able to use QPushButton (if that is what you mean by 'button widget') directly, right?

    You could also use an event filter. Either on one or more specific widgets in the widget hierarchy, or on the entire application. If you really want to get ALL those key events, an event filter on the application should do it. But are you sure you really want to filter out ALL of them...?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.