问题
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 .
回答1:
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.
来源:https://stackoverflow.com/questions/23911433/qml-cant-get-mouse-released-event-when-i-dont-accept-mouse-pressed-event