qtevent-handlingqmlmouseeventqquickitem

QML forward mouse events to the parent


I have TableView inside another Item that have MouseArea. How can I pass all mouse events to the parent of the TableView (or in another words, make it transparent for mouse events) ?


Solution

  • If you want to get the mouse events just in the parent item, stack the mouse area above the table.

    With propagateComposedEvents you can achieve, that both gets the mouse event.

    Small example:

    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")
                       }
        }
    }