Search code examples
qtfocusqwidget

move focus with keys other than tab key


Suppose i have a Qwidget with three child widgets in it. How can i move focus between child widgets by pressing keys other than tab key? is there any built-in mechanism in Qt?


Solution

  • You can use setFocus() method of widgets. Note that you can only set a focus for a widget that accepts focus. For example, buttons, text edits accepts focus by default. Frames, labels do not. You can make widgets accepting focus using setFocusPolicy() but usually that is not required.

    If your want to attach this action to another key or combination, you can use QShortcut. Also you can add an event filter to target widgets and detect keyboard events.