How to include child mouse hover events in the parent MouseArea using QML?

前端 未结 4 2013
自闭症患者
自闭症患者 2021-02-02 09:20

I want to implement the following scenario in QML.

\"Scenario\"


Here is a sample/simplified delegat

4条回答
  •  情书的邮戳
    2021-02-02 09:55

    Iv'e tried a few things but it does not seem possible to hover over two MouseArea simultaneously. The preventStealing and propagateComposedEvents seem to only work when you have a click event. But from the inner MouseArea you can trigger the entered() signal of the other one. Something like this:

    import QtQuick 2.1
    
    Rectangle {
        width: 500
        height: 500
    
        Rectangle {
            width:300
            height: 300
            color: "red"
    
            MouseArea {
                id: big
                anchors.fill: parent
                hoverEnabled:true
                onEntered: {
                    console.log("ENTERED BIG mousearea");
                }
                onExited: {
                    console.log("EXITED BIG mousearea");
                }
            }
    
            Rectangle {
                anchors.centerIn: parent
                height: 100
                width: 100
                color: "green"
    
                MouseArea {
                    anchors.fill: parent
                    hoverEnabled:true
                    onEntered: {
                        console.log("ENTERED small mousearea");
                        big.entered();
                    }
                    onExited: {
                        console.log("EXITED small mousearea");
                        big.exited();
                    }
                }
            }
        }
    }
    

    The issue is that the exited() signal from the containing MouseArea will be called before calling the entered() back again. So you might need to "delay" the change of state in exited() just to make sure you really want to hide your action buttons. Another solution would be to save the current mouse position and hide the buttons ONLY if exited() is called with the mouse on one of its border.

提交回复
热议问题