【问题标题】:Sending mouse events to a QtQuick window in a virtual reality OpenGL scene将鼠标事件发送到虚拟现实 OpenGL 场景中的 QtQuick 窗口
【发布时间】:2016-12-30 13:25:06
【问题描述】:

我目前正在开发一个基于 OpenGL 的虚拟现实项目。因为我还需要某种形式的用户界面,所以我认为将 QtQuick 窗口集成到场景中是个好主意。将窗口绘制到纹理上没有问题(我使用了this example),但我很难发送鼠标事件以便我的控制器可以与之交互。

Here is a quick example video on YouTube. 在这个例子中,当我将鼠标悬停在嵌入的 GIF 上时,它的动画应该停止。这在普通的 QML 应用程序中有效,但在我手动发送 MouseMove 事件时无效。 鼠标在窗口内的位置是已知的(视频中的红线表示一个交叉点),我目前正在通过

发送事件
QQuickWindow::sendEvent(QQuickItem* item, QEvent*)

其中 item 是 qml 源代码中的根 Rectangle

import QtQuick 2.3
import QtQuick.Controls 2.0

Rectangle {
    color: mouseArea.containsMouse ? "red" : "white"
    width: 600
    height: 400

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true

        AnimatedImage {
            anchors.fill: parent
            paused: mouseArea.containsMouse
            source: "test.gif"
        }
    }
}

每次找到光线和窗口之间的交点时我发送的事件是通过以下方式完成的:

QMouseEvent* mouseMoveEvent = new QMouseEvent(
    QEvent::MouseMove, // wrong event?
    cursorPosition, cursorPosition, // Note: cursorPosition := the calculated cursor coordinates within the window
    Qt::MouseButton::NoButton,
    Qt::MouseButtons(), // is this right?
    Qt::KeyboardModifier::NoModifier);
window->sendEvent(rootItem, mouseMoveEvent); // Note: window is my QQuickWindow, rootItem is the root Rectangle

我不是 QML 方面的专家(很少使用它),如果有人对如何解决这个问题提出建议,我将不胜感激。我的猜测是,我既没有使用正确的事件,也没有正确发送它们。

【问题讨论】:

  • 你是如何处理鼠标事件的?您是否首先检查过事件是否正在传播?检查doc.qt.io/qt-5/qwidget.html#mouseTracking-prop
  • 我没有使用 QWidget,我的问题是关于创建我自己的事件(每当我检查来自控制器的光线和窗口之间的交叉点),然后将其发送到我的窗口。解决方案是使用 QApplication::instance()->sendEvent(..) 而不是 QQuickWindow::sendEvent(..)。

标签: c++ opengl qt5 qtquick2 virtual-reality


【解决方案1】:

我自己找到了解决方案。而不是使用 QQuickWindow::sendEvent(..) 我不得不简单地使用 QApplication()::instance()->sendEvent(..)。

【讨论】:

  • 很确定你也可以使用后一种方法,只要事件被传播到处理程序。
  • 对我有用的是使用QCoreApplication::sendEvent(quick_window, event);
猜你喜欢
  • 2014-10-04
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 2018-09-30
相关资源
最近更新 更多