简体   繁体   English

QQuickView:用C ++处理鼠标事件

[英]QQuickView: handling mouse events in C++

I render my 3d model under qml controls using QQuickView::beforeRendering event. 我使用QQuickView :: beforeRendering事件在qml控件下渲染我的3d模型。 I want to do my mouse events handling in C++ if user clicks outside any of qml controls/ How can I found out in QQuickView::mousePressEvent that mouse is pressed outside qml controls? 如果用户点击任何qml控件之外我想在C ++中执行我的鼠标事件处理/如何在QQuickView :: mousePressEvent中找到鼠标在qml控件外被按下?

I think it's easier to do it with a custom QQuickItem , because doing it with a custom QQuickView apparently means that you get the events before they reach any of the items. 我认为使用自定义QQuickItem更容易,因为使用自定义QQuickView显然意味着您可以在事件到达任何项目之前获取事件。

Here's an example: 这是一个例子:

#include <QtQuick>

class MyItem : public QQuickItem
{
public:
    MyItem() {
        setAcceptedMouseButtons(Qt::AllButtons);
    }

    void mousePressEvent(QMouseEvent *event) {
        QQuickItem::mousePressEvent(event);
        qDebug() << event->pos();
    }
};

int main(int argc, char** argv)
{
    QGuiApplication app(argc, argv);

    QQuickView *view = new QQuickView;
    qmlRegisterType<MyItem>("Test", 1, 0, "MyItem");
    view->setSource(QUrl::fromLocalFile("main.qml"));
    view->show();

    return app.exec();
}

Put the custom item at the bottom of the scene and it will get all of the unhandled mouse events: 将自定义项放在场景的底部,它将获得所有未处理的鼠标事件:

import QtQuick 2.3
import QtQuick.Controls 1.0
import Test 1.0

Rectangle {
    width: 400
    height: 400
    visible: true

    MyItem {
        anchors.fill: parent
    }

    Button {
        x: 100
        y: 100
        text: "Button"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM