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

前端 未结 4 1909
自闭症患者
自闭症患者 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:48

    Try this:

    • add a signal to the inner area that's emitted on mouse enter.
    • Connect the signal to the outer area.
    • The signal causes the outer area to enter the hovered state.

    Mouse exit on both will still cancel hover state. As you move the mouse off the controls it should work correctly without any extra code

    0 讨论(0)
  • 2021-02-02 09:51

    make states for each state of the elements in the View then you can use things like if statements or case statements to change these properties In Other words, Try not to set your elements up to work on MouseArea but on properties And set the Elements properties to work on the set properties I hope that this helps if not here is example:

    EDIT I added the color to be transparent. if there is no mouse what so ever. If I was using a Image I would use opacity then add a bunch of Behaviors also But this is a working

    example

    import QtQuick 2.0
    Rectangle {
        width: 360
        height: 360
        property string state1:"OutMouse"
        property string state2: "OutMouse"
        property string state3: "OutMouse"
        property string state4: "OutMouse"
        Rectangle{
            id:blueRec
            width: parent.width
            height: parent.height / 6
            color: state1 === "InMouse" ? "blue" : "green"
            MouseArea{
                anchors.fill: blueRec
                hoverEnabled: true
                onEntered: state1 = "InMouse"
                onExited: {
                    if (state1 === state2 || state3 || state4){
                        state1 = "InMouse"
                    }
                    if(state1 !== state2 || state3 || state4)
                    {
                        state1 = "OutMouse"
                    }
                }
            }
            Text {
                text: state1=== "InMouse"? qsTr("foo") :"bar"
                anchors.centerIn: blueRec
            }
            Row{
                width: parent.width
                height: parent.height / 4
    
                spacing: 2
                anchors{
                    left: parent.left
                    verticalCenter:  blueRec.verticalCenter
                    leftMargin: blueRec.width / 12
                }
                Rectangle{
                    id: rec1
                    height: parent.height;
                    width: height
                    color: {
                        if  ( state3 === "InMouse")
                            return "gray"
                        if (state1 === "OutMouse")
                            return "transparent"
                        else
                            return "white"}
                    MouseArea{
                        id: rec1M
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered:{
                            state1 = "InMouse"
                            state2 = "InMouse"
                        }
                        onExited: state2 = "OutMouse"
                    }
                }
    
                Rectangle{
                    id: rec2
                    height: parent.height ;
                    width: height
                    color: {
                        if  (state3 === "InMouse")
                            return "gray"
                        if (state1 === "OutMouse")
                            return "transparent"
                        else
                            return "white"
                    }
                    MouseArea{
                        id: rec2M
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered:{
                            state1 = "InMouse"
                            state3 = "InMouse"
                        }
                        onExited: state3 = "OutMouse"
                    }
                }
    
                Rectangle{
                    id: rec3
                    height: parent.height;
                    width: height
                    color:{
                        if  (state4 === "InMouse")
                            return "gray"
                        if (state1 === "OutMouse")
                            return "transparent"
                        else
                            return "white"
                    }
                    MouseArea{
                        id:  rec3M
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered:{
                            state4 = "InMouse"
                            state1 = "InMouse"
                        }
                        onExited: state4 = "OutMouse"
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-02 09:52

    I was faced by this same problem, and came across the answer in the QtQuick 5.0 documentation for MouseArea. The answer to this is actually quite simple.

    If you want to include child mouse hover events in your parent MouseArea, make you child MouseArea a child of the parent MouseArea:

    MouseArea {
        id: parent
    
        MouseArea {
            id: child
        }
    }
    

    Since I have a custom Widget type that would be used as the parent view, I ended up with the default property being the children of the MouseArea:

    Item {
        default property alias children: mouseArea.data
    
        MouseArea {
            id: mouseArea
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题