QML - 当我不接受鼠标按下事件时,无法获取鼠标释放事件 [英] QML - Can't get mouse released event when I don't accept mouse pressed event

查看:1190
本文介绍了QML - 当我不接受鼠标按下事件时,无法获取鼠标释放事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的onPressed事件发送到QML树中的后续对象(我已经指示mouse.accepted = false和propagateComposedEvents:true),但是我想保持上一级的事件在顶级元素上工作..
这里的代码:

 项目{

width:200
height: 200

矩形{
z:1
anchors.fill:parent
MouseArea {
anchors.fill:parent
propagateComposedEvents:true
onPressed:{
console.log(R1 pressed)
mouse.accepted = false
}
onReleased:{
console.log )
mouse.accepted = false
}
}
}

矩形{
z:0
anchors.fill:父
MouseArea {
anchors.fill:parent
propagateComposedEvents:true
onPressed:{
console.log(R2 press ed)
}
onReleased:{
console.log(R2 released)
}
}
}


我期待看到的内容:



qml:R1按
qml:R2按
qml:R1发布
qml:R2发布



我得到的: / p>

qml:R1按
qml:R2按
qml:R2发布



我可以解决吗



提前感谢您的帮助。

解决方案

使用 propagateComposedEvents 将不起作用,因为它的行为仅针对组合事件定义,这意味着点击 doubleClicked pressAndHold 。所以当您在R1的 onPressed 处理程序中设置 mouse.accepted = false 时,这与 propagateComposedEvents



设置 mouse.accepted = false 应该根据文档做:


下一次按下按钮
之前,不会再发送任何进一步的事件到这个MouseArea。


在我看来,只能对最上面的MouseArea上的鼠标输入做出反应。只要R1和R2完全相互重叠,你就会做出一些它根本不是的东西。如果R1小于R2,并且您想在R1的处理程序中为R2做一些事情,只需将R2代码移动到一个函数,并从MouseAreas的事件处理程序中调用它。或者只需使用绑定到Mouseareas的按下状态。


I would like to dispatch my onPressed event to subsequent objects in my QML tree, (I have indicate mouse.accepted = false and propagateComposedEvents: true ) but I want to keep the onreleased event working on the top level element .. Here the code :

Item {

    width: 200
    height: 200

Rectangle {
    z: 1
    anchors.fill: parent
    MouseArea{
        anchors.fill: parent
        propagateComposedEvents: true
        onPressed: {
            console.log("R1 pressed")
            mouse.accepted = false
        }
        onReleased: {
            console.log("R1 released")
            mouse.accepted = false
        }
    }
}

Rectangle {
    z: 0
    anchors.fill: parent
    MouseArea{
        anchors.fill: parent
        propagateComposedEvents: true
        onPressed: {
            console.log("R2 pressed")
        }
        onReleased: {
            console.log("R2 released")
        }
    }
}

}

What I expect to see :

qml: R1 pressed qml: R2 pressed qml: R1 released qml: R2 released

What I get :

qml: R1 pressed qml: R2 pressed qml: R2 released

How can I solve that ?

Thanks in advance for your help .

解决方案

Using propagateComposedEvents will not work because its behavior is only defined for composed events, which means one of clicked, doubleClicked and pressAndHold. So when you set mouse.accepted = false in R1's onPressed handler, this has nothing to do with propagateComposedEvents.

Setting mouse.accepted = false instead does what it is supposed to do according to the documentation:

no further events will be sent to this MouseArea until the button is next pressed.

In my opinion, one should only react to mouse inputs on the topmost MouseArea. As long as R1 and R2 completely overlap with each other, you are making R2 something that it simply isn't. In case R1 is smaller than R2 and you want to do something for R2 in R1's handlers, just move the R2 code to a function and call it from both MouseAreas' event handlers. Or just use bindings to both Mouseareas' pressed state.

这篇关于QML - 当我不接受鼠标按下事件时,无法获取鼠标释放事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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