Intercepting Tab key press to manage focus switching manually

前端 未结 4 1782
一向
一向 2020-12-16 22:58

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:



        
相关标签:
4条回答
  • 2020-12-16 23:11

    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.

    0 讨论(0)
  • 2020-12-16 23:13

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

    0 讨论(0)
  • 2020-12-16 23:25

    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)

    0 讨论(0)
  • 2020-12-16 23:30

    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.

    0 讨论(0)
提交回复
热议问题