c++ - QtQuick 按键事件传播

标签 c++ qt qtquick2

我有 Qt GUI 应用程序。主窗口包含使用窗口容器放置在其上的 QtQuick 组件:

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

我想处理整个窗口下的所有按键事件。但是我遇到了一个问题,即当 QtQuick 组件获得焦点时我无法处理关键事件,即使我已将窗口设置为它的父窗口也是如此。

documentation说这种行为是预期的:

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.

我的问题是:即使焦点被QtQuick组件获取,有没有办法在整个窗口下处理按键事件?

GitLab 提供了最小且完整的示例.

最佳答案

一个可能(但丑陋)的解决方案是:

  1. 在 QML 中处理按键事件;
  2. 通过信号从 QML 通知 C++ 代码;
  3. 在C++代码中生成按键事件;
  4. 在公共(public)事件过滤器中处理 native 事件和重新生成的事件。

示例位于 GitLab .

关于c++ - QtQuick 按键事件传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38096428/

相关文章:

c++ - 如何在 C++/openFrameworks 中的文件名字符串中使用变量 int

c++ - 错误 C3867 : function call missing argument list

c++ - 为什么我的 QML textArea 没有 append ?

qt - 在 QML 中使用模型的大小

c++ - 如何重新安排并发任务?

c++ - 有没有办法确保在 C++ 中传递的模板参数是一个迭代器?

javascript - QT QWebEngine 滚动后渲染?

c++ - 如何从 QT 中检索 Windows 中的文件属性元数据?

c++ - 从运行 QML QtQuick2 引擎的不同线程更新 c++ 模型的行为

qt - QML 的组件和实例化器之间的区别?