QML 将鼠标事件转发给父级

问题描述 投票:0回答:1

我在另一个有

TableView
Item
中有
MouseArea
。如何将所有鼠标事件传递给
TableView
的父级(或者换句话说,使其对鼠标事件透明)?

qt event-handling qml mouseevent qquickitem
1个回答
0
投票

如果只想获取父项中的鼠标事件,请将鼠标区域堆叠在表格上方。

使用

propagateComposedEvents
你可以实现,两者都获取鼠标事件。

小例子:

Rectangle {

    color: "red"
    anchors.fill: parent

    Rectangle {
        color: "green"
        anchors.fill: parent
        anchors.margins: 20

        MouseArea {
            anchors.fill: parent
            onClicked: console.log("green")
        }
    }

    MouseArea {
        anchors.fill: parent

        // if you activate this, both mouse areas get the click
        propagateComposedEvents: true

        onClicked: (mouse) => {
                       // true: only one mouse area is getting the event if propagateComposedEvents is set
                       // false: both mouse areas are getting the event if propagateComposedEvents is set
                       mouse.accepted = false;
                       console.log("red")
                   }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.