簡體   English   中英

QQuickView:用C ++處理鼠標事件

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

我使用QQuickView :: beforeRendering事件在qml控件下渲染我的3d模型。 如果用戶點擊任何qml控件之外我想在C ++中執行我的鼠標事件處理/如何在QQuickView :: mousePressEvent中找到鼠標在qml控件外被按下?

我認為使用自定義QQuickItem更容易,因為使用自定義QQuickView顯然意味着您可以在事件到達任何項目之前獲取事件。

這是一個例子:

#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();
}

將自定義項放在場景的底部,它將獲得所有未處理的鼠標事件:

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