Important: Please read the Qt Code of Conduct -

focus and keyboard events in Widgets/Quick Applications

  • I have an application that uses movable "modules", window-like widgets of different types. They all have a "titlebar" (another widget), "borders" (another outer widget) and an inner content widget. The content of some is implemented using QWidgets, the content of one uses a QQuickView, let's call it QuickUi.

    In QuickUi, I have various components, including a GridView. When the focus is on QuickUi (i.e. the user has most recently clicked somewhere in QuickUi), I want Ctrl+A to select all GridView tiles. When the focus is, i.e. in another module's text field, it should, of course, exhibit default behaviour (text selection).

    My first approach was using Actions in QuickUi. The problem is that when I click on QuickUi's module's borders widget and then back inside the QQuickView, in many cases the actions don't respond anymore. I then have to click in some other module and back into QuickUi...

    Second approach was to use a global event filter on the application object, and then distribute events via signals/slots. Works fine, except if I call a slot in QuickUi on Ctrl+A, I have to decide whether I have focus (and only trigger tile selection if I do). But how do I know that? focus and activeFocus on the QuickUi's root element is always false. I also don't get any focusInEvent calls in any of the container widgets...

    Is there any way I can find out which QWidget has the active focus, i.e. received the most recent mouse click?

  • I just did some more experiments and it seems that if I subclass QQuickView and put debug outputs in the focusInEvent and focusOutEvent handlers, there are cases in which clicking any element in the QML structure gives the quickview focus, but in other cases (e.g. when I click on one of the "border" widgets, not necessarily its own one, and then back into it) it doesn't get focus. How is that possible?