QQuickView:在C ++中处理鼠标事件 [英] QQuickView: handling mouse events in C++

查看:202
本文介绍了QQuickView:在C ++中处理鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在qml控件下使用QQuickView :: beforeRendering事件渲染我的3d模型。
如果用户点击任何qml控件之外,我想在C ++中执行我的鼠标事件处理/我如何在QQuickView :: mousePressEvent中发现鼠标在qml控件外按下?


<我认为使用自定义 QQuickItem 更容易做到这一点,因为使用自定义<$ c>



这里有一个例子:

$

b
$ b

  #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
}

按钮{
x:100
y:100
text:Button
}
}


I render my 3d model under qml controls using QQuickView::beforeRendering event. 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?

解决方案

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.

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"
    }
}

这篇关于QQuickView:在C ++中处理鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆