Search code examples
qtqt5qwidget

How to capture Tab Key in Qt widget


I want to implement my own sequence for changing the focus of the active child widget using the Tab key. How to capture the Tab Key press event? I am using Qt5.2


Solution

  • If you want to change focus with Tab , you don't need to do those works, Qt has it as a feature.

    First: set the desired widgets to be Qt::TabFocus or Qt::StrongFocus by QWidget::setFocusPolicy( Qt::FocusPolicy policy )

    For example, if you want to rotate between 3 QLineEdit and 1 QCombobox, you have to assure that their focus policy have been set right. (Normally either Qt::TabFocus or Qt::StrongFocus will be set as default, but sometimes you might want to escape some widgets from being tabbed)

    Second: go to designer mode and click "Edit Tab Order" to enter the tab-order editing mode

    enter image description here

    Third: After seeing the numbers, click on them until you got the desired sequence order.

    (Picture from Qt official site)

    enter image description here