ska*_*esh 7 qt event-propagation qml
我有两个矩形,每个都有一个TapHandler. 矩形 A 是矩形 B 的父级
如何配置 A 和 B,以便单击 B 时,EventPoint不会传播到onSingleTappedA 的处理程序?
EventPoint文档建议将其属性设置accepted为 true:
将 Accepted 设置为 true 可防止事件传播到 PointerHandler 的 Item 下面的 Item。
然而,同时文档声明这accepted是一个只读属性,这没有多大意义(我猜文档已经过时或根本就是错误的)。
测试代码:
Rectangle {
id: a
width: 200
height: 200
color: "yellow"
TapHandler {
onSingleTapped: console.log("A tapped")
}
Rectangle {
id: b
color: "blue"
width: 100
height: 100
TapHandler {
onSingleTapped: function(eventPoint) {
// Stop propagation.
eventPoint.accepted = true
console.log("B tapped")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:设置gesturePolicyB 的 来TapHandler.ReleaseWithinBounds防止 A 接收事件。不确定这是否真的是最好的解决方案
对于Handlers来说,整体event传递给每个handler;因此,处理者接受个体points,而不是整体event。一般来说,接受所有点意味着接受整个事件,但也可能是一个处理程序接受一些点,而另一个处理程序接受其他点。直到所有点都为 时,交付才为 \xe2\x80\x9cdone\xe2\x80\x9d accepted。
看起来grabPermissions没有 a 的设置gesturePolicy不会执行预期的操作..捕获事件并防止传播到其他项目。
Rectnagle将b (a 的孩子)更改TapHandler为 hasgesturePolicy: TapHandler.ReleaseWithinBounds TapHandler.WithinBounds似乎是正确的方法aaccept,换句话说,它接受该点,这意味着该事件不会传播到父 Rectangle 的 TapHandler!
Rectangle {\n id: b\n z:2\n color: "blue"\n width: 100\n height: 100\n TapHandler {\n gesturePolicy: TapHandler.ReleaseWithinBounds | TapHandler.WithinBounds\n grabPermissions: PointerHandler.CanTakeOverFromAnything | PointerHandler.CanTakeOverFromHandlersOfSameType | PointerHandler.CanTakeOverFromItems\n | PointHandler.ApprovesTakeOverByAnything | PointHandler.ApprovesCancellation\n onSingleTapped: function(eventPoint) {\n // Stop propagation.\n eventPoint.accepted = true // this is just a confirmation!\n console.log("B tapped")\n }\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n
| 归档时间: |
|
| 查看次数: |
1550 次 |
| 最近记录: |