如何传播MouseEvent,例如onEntered / onExited?

时间:2015-11-16 12:56:33

标签: qml

根据Qt documentation on MouseArea,只有'组成' clicked等事件可以传播到z顺序较低的项目。

如何为简单的鼠标事件(例如onEntered或onExited)复制相同的行为?

e.g。我有两个矩形,都有一个MouseArea,上面的矩形覆盖下方矩形的右下角。如果我点击任一矩形的任何地方,包括重叠区域,两个矩形' MouseAreas将收到onClicked信号,因为我已使用propagateComposedEvents: true并在upperRect的处理程序中设置mouse.accepted = false。但是onEntered仅适用于lowerRect的非重叠部分。

Rectangle {
    id: lowerRect
    x: 0
    y: 0
    width: 100
    height: 100
    color: "blue"

    MouseArea {
        anchors.fill: parent
        onClicked: console.log("lowerRect was clicked")
        hoverEnabled: true
        onEntered: console.log("lowerRect was entered")
    }
}

Rectangle {
    id: upperRect
    x: 50
    y: 50
    width: 50
    height: 50
    color: "green"
    opacity: 0.8

    MouseArea {
        anchors.fill: parent
        propagateComposedEvents: true
        onClicked: {
            mouse.accepted = false
            console.log("upperRect was clicked")
        }
        hoverEnabled: true
        onEntered: console.log("upperRect was entered")
    }
}

1 个答案:

答案 0 :(得分:1)

您可以手动呼叫信号。类似于myMouseArea.entered()myMouseArea.exited()(其中myMouseAreaid或对MouseArea对象的引用)的方法可行。