2
votes

I have Qt GUI application. Main window contains QtQuick component which placed on it using window container:

QQuickView * view = new QQuickView ();
QWidget * container = QWidget::createWindowContainer (view, this);

I'd like to handle all key press events under whole window. But I faced with problem that I can't handle key events when focus acquired by QtQuick component even if I've set the window as parent for it.

The documentation say that such behaviour is expected:

QWidget *QWidget::createWindowContainer(QWindow *window, QWidget *parent = > Q_NULLPTR, Qt::WindowFlags flags = Qt::WindowFlags())
...
The window container has a number of known limitations:
...
Focus Handling; It is possible to let the window container instance have any focus policy and it will delegate focus to the window via a call to QWindow::requestActivate(). However, returning to the normal focus chain from the QWindow instance will be up to the QWindow instance implementation itself. For instance, when entering a Qt Quick based window with tab focus, it is quite likely that further tab presses will only cycle inside the QML application. Also, whether QWindow::requestActivate() actually gives the window focus, is platform dependent.

My question is: is there way to handle key press event under whole window even if focus is acquired by QtQuick component?

Minimal and complete example available at GitLab.

1

1 Answers

0
votes

One possible (but ugly) solution is:

  1. Handle key press event in QML;
  2. Notify C++ code from QML by signal;
  3. In C++ code generate key press event;
  4. Handle native event and regenerated event in common event filter.

Example available at GitLab.