如何使用QML在父MouseArea中包含子鼠标悬停事件?

时间:2013-08-08 20:13:06

标签: qt qml qt5 qtquick2

我想在QML中实现以下场景。

Scenario


以下是ListView元素的示例/简化委托:

Component {
    Item {
         id: container
         MouseArea {
         anchors.fill: parent
         hoverEnabled: true

         onClicked: {
             container.ListView.view.currentIndex = index
             container.forceActiveFocus();
         }
         onEntered: {
             actionList.state = "SHOW";
             myItem.state = "HOVER"
         }
         onExited: {
             actionList.state = "HIDE";
             myItem.state = "NORMAL"
         }
         Rectangle {
             id: myItem
             color: "gray"
             anchors.fill: parent
             Row {
                 id: actionList
                 spacing: 5; anchors.fill: parent
                 Image {
                     id: helpAction
                     source: ""    //Some image address
                     width: 16; height: 16; fillMode: Image.PreserveAspectFit
                     states: [
                         State {
                             name: "NORMAL"
                             PropertyChanges { target: helpAction; opacity: 0.7 }
                         },
                         State {
                             name: "HOVER"
                             PropertyChanges { target: helpAction; opacity: 1.0 }
                         }
                     ]
                     MouseArea {
                         hoverEnabled: true
                         anchors.fill: parent

                         onEntered: {
                             parent.state = "HOVER";
                         }
                         onExited: {
                             parent.state = "NORMAL";
                         }
                     }
                     states: [
                         State {
                             name: "SHOW"
                             PropertyChanges { target: actionList; visible: false }
                         },
                         State {
                             name: "HIDE"
                             PropertyChanges { target: actionList; visible: true }
                         }
                     ]
                 }

                 //Other action buttons...

                 states: [
                     // `NORMAL` and `HOVER` states definition here...
                 ]
             }
         }
    }
}

但我对MouseArea有疑问 内部MouseArea(actionButton)无法正常处理entered事件。当鼠标进入操作按钮时,外部MouseArea会触发exited事件。

我的代码中有错误吗?更一般地说,我如何在QML中实现这样的场景?

4 个答案:

答案 0 :(得分:23)

我遇到了同样的问题,并在QtQuick 5.0 documentation for MouseArea中遇到了答案。答案实际上非常简单。

如果您想在您的父MouseArea中添加子鼠标悬停事件,请让您的孩子MouseArea成为父MouseArea的孩子:

MouseArea {
    id: parent

    MouseArea {
        id: child
    }
}

由于我有一个自定义Widget类型可用作父视图,因此我最终将default属性作为MouseArea的子项:

Item {
    default property alias children: mouseArea.data

    MouseArea {
        id: mouseArea
    }
}

答案 1 :(得分:4)

为视图中元素的每个状态创建状态,然后您可以使用if语句或case语句等内容来更改这些属性。换句话说,尽量不要将元素设置为在MouseArea上工作但在属性上设置并设置要处理set属性的元素属性我希望这有助于(如果不是)示例:

编辑我添加的颜色是透明的。如果没有老鼠那么多。如果我使用的是Image,我会使用不透明度,然后添加一堆行为,但这是一个有效的

例如

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

答案 2 :(得分:3)

我试过了一些事情,但似乎不可能同时悬停在两个MouseArea上。 preventStealingpropagateComposedEvents似乎仅在您有点击事件时才有效。但是从内部MouseArea你可以触发另一个entered()信号。像这样:

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();
                }
            }
        }
    }
}

问题是,在再次调用exited()之前,将调用包含MouseArea的{​​{1}}信号。因此,您可能需要“延迟”entered()中的状态更改,以确保您确实要隐藏操作按钮。另一个解决方案是保存当前鼠标位置并仅在使用鼠标在其边框上调用exited()时隐藏按钮。

答案 3 :(得分:1)

试试这个:

  • 向鼠标输入时发出的内部区域添加信号。
  • 将信号连接到外部区域。
  • 信号导致外部区域进入悬停状态。

两者上的鼠标退出仍将取消悬停状态。当您将鼠标移离控件时,它应该可以正常工作而无需任何额外的代码