Handling TAB-presses exclusively
-
Following situation:
My UI has top-level tabs that can contain multiple child widgets several levels deep. I want to use (CTRL-)TAB to navigate between those top-level tabs, and only for that.
I've overwritten the keyPressEvent(QKeyEvent)*-function for that purpose. This catches every keypress correctly EXCEPT for TAB. I assume this is because TAB is, by default, used to switch focus between all widgets on the screen (which I can observe). Apparently this also happens before my function can catch the TAB press, so it never receives it.
How can I correct this behavior? My first idea was to loop through all of the widgets and set their FocusPolicy to NoFocus, but that seems too hacky and I'm not even sure whether it would work.
The second idea was to make sure that the QTabWidget containing the top-level tabs never loses its focus, so I can just utilize its built-in TAB-navigation. But there seems to be no way to force the focus on a single widget without wild hacks (like listening for the focusChangedEvent and then manually returning the focus to the QTabWidget...).
It feels like I'm going all wrong about this, can anyone help me out here?
There's gotta be a better way!
-
@qwasder85 said in Handling TAB-presses exclusively:
I cannot comment on whether your approach is best or not. But for dealing with Tab correctly, have you read through https://stackoverflow.com/questions/18160051/intercepting-tab-key-press-to-manage-focus-switching-manually (or the thread https://forum.qt.io/topic/30547/solved-intercepting-tab-key-press-to-manage-focus-switching-manually in this forum) ? Both the answers there, https://stackoverflow.com/a/18161997/489865 and https://stackoverflow.com/a/21351638/489865, look like possible solutions?