Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Switching Keyboard Focus Away from QMdiArea



  • Hello all,

    I'm having a problem with keyboard focus in relation to a QMdiArea. My problem is described below.

    My application uses a subclass of QMainWindow with a QMdiArea as the central widget and several QDockWidgets surrounding the central widget. Prior to using QMdiArea as the central widget we were using a QWorkspace. We upgraded since QWorkspace is now deprecated.

    In the previous version, a user could use the "Tab" and "Shift+Tab" keys in order to change keyboard focus between the QDockWidgets and the QWorkspace. Essentially users could "Tab" around the application using only the keyboard.

    Once we switched to a QMdiArea as the central widget this was no longer possible. If the keyboard focus happens to be in one of the QDockWidgets then the "Tab" and "Shift+Tab" keys work as expected, however if the keyboard focus reaches the QMdiArea, the "Tab" and "Shift+Tab" keys no longer work and the keyboard focus is stuck on the QMdiArea and the user can only get back to the QDockWidgets by clicking them with the mouse.

    I was looking through the Qt source code at the qmdiarea.cpp file and I see there is an event filter where the Qt::Key_Tab and Qt::Key_Backtab key sequences call a method that highlights the next visible child of QMdiArea.

    To work around this, I created an event filter in my subclass of QMainWindow and installed it on the QMdiArea at construction time. I was able to get the "Tab" key working again mostly. It now will switch the focus to one of the QDockWidgets that I've specified for that event, however, the "Shift+Tab" combination still does not work. I would prefer to not hardcode the widget that should receive focus but the QMainWindow::focusNextChild() method seems to call the child windows of the QMdiArea instead of one of the QDockWidgets within the QMainWindow. Anyway, I digress.

    My event filter in my QMainWindow subclass is being triggered on a "Shift+Tab" key press, but it's being triggered always instead of only when the QMdiArea has the focus like it does when the "Tab" key is pressed.

    To make a long story short, I would like to know if there is a specific reason why the "Tab" and "Shift+Tab" keys work the way they do in relation to the QMdiArea and if there is any way I can have them behave the way they used to when the central view was a QWorkspace.

    There seems to be no way to switch keyboard focus out of the QMdiArea completely. Just as a side note, the "Ctrl+Tab" and "Ctrl+Shift+Tab" keys work as expected, they change the focus of the child views inside of the QMdiArea.

    Thanks in advance for any help provided.


Log in to reply