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

前端 未结 4 1908
自闭症患者
自闭症患者 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: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"
                    }
                }
            }
        }
    }
    

提交回复
热议问题