【问题标题】:QML InputHandler stop propagation of eventQML InputHandler 停止传播事件
【发布时间】:2020-12-28 08:51:56
【问题描述】:

我有两个矩形,每个矩形都有一个TapHandler。矩形 A 是矩形 B 的父级
怎么配置A和B,使得点击B时EventPoint不会传播到A的onSingleTapped处理函数?

EventPoint docs 建议将其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")
            }
        }
    }
}

更新:将 B 的 gesturePolicy 设置为 TapHandler.ReleaseWithinBounds 可防止 A 接收事件。不确定这是否真的是最好的解决方案

【问题讨论】:

    标签: qt qml event-propagation


    【解决方案1】:

    对于Handlers,整个event被传递给每个handler;因此处理程序接受单个points,而不是整个event。一般来说,接受所有点意味着接受整个事件,但可能是一个处理程序接受一些点,而另一个处理程序接受其他点。直到所有点都为accepted,才“完成”交付。

    看起来在没有gesturePolicy 的情况下设置grabPermissions 并没有达到预期的效果.. 抓住事件并防止传播到其他项目

    Rectnagle b(a 的孩子)TapHandler 更改为gesturePolicy: TapHandler.ReleaseWithinBounds TapHandler.WithinBounds 似乎是aaccept 的正确方法,换句话说,它接受了这一点,这意味着事件不会传播到的 TapHandler父矩形!

        Rectangle {
            id: b
            z:2
            color: "blue"
            width: 100
            height: 100
            TapHandler {
                gesturePolicy: TapHandler.ReleaseWithinBounds | TapHandler.WithinBounds
                grabPermissions: PointerHandler.CanTakeOverFromAnything | PointerHandler.CanTakeOverFromHandlersOfSameType | PointerHandler.CanTakeOverFromItems
                                 | PointHandler.ApprovesTakeOverByAnything | PointHandler.ApprovesCancellation
                onSingleTapped: function(eventPoint) {
                    // Stop propagation.
                    eventPoint.accepted = true // this is just a confirmation!
                    console.log("B tapped")
                }
            }
        }
    

    进一步...narkive interset group

    【讨论】:

    • 感谢您的详细回答。我也发现了这一点(请参阅我的更新)。我暂时接受你的回答
    猜你喜欢
    • 1970-01-01
    • 2011-01-05
    • 2012-10-10
    • 2011-04-06
    • 2016-05-18
    • 1970-01-01
    • 2011-12-09
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多