qml 鼠标点击_QML - 当我不接受鼠标按下事件时,无法获取鼠标释放事件

在QML中,当顶级元素设置`mouse.accepted = false`来传递按下事件时,释放事件不再触发。文章讨论了如何在保持顶部元素的`onReleased`事件工作的同时,允许鼠标事件穿透到下层对象。解决方案包括理解`propagateComposedEvents`的行为限制以及在适当情况下使用`mouse.accepted`属性。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值