2

我遇到了嵌套MouseAreas 的问题。当我单击红色Rectangle时,它会触发exited顶部的事件Rectangle(至少对我而言)。这是预期的行为吗?如果是这样,是否有解决方法?

编辑

澄清
我想要的是能够Rectangle在单击孩子时保持悬停在顶部Rectangle(顶部Rectangle应该保持blue)。如果我想在悬停时隐藏和取消隐藏按钮等,这很有用,这是 Web 界面 (HTML/CSS) 中的常见做法。现在这样做会导致奇怪的效果,因为单击entered信号显示的按钮会在您单击它们时消失。

编辑

我用想要的行为做了一个 Javascript 演示: https ://jsfiddle.net/uo4vousu/ 即使你点击红色矩形,父级保持蓝色,这就是我想要的。在 QML 示例中,父级变为绿色。我更新了 QML 代码以匹配 jsfiddle。

编辑

这可能是一个 linux 唯一的问题。

这是一个例子:

Rectangle {
    id: top
    width: 100
    height: 32
    anchors.centerIn: parent
    color: "green"

    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onExited: top.color = "green"
        onEntered: top.color = "blue"

        Rectangle {
            id: child
            width: 16
            height: 16
            anchors.centerIn: parent
            color: "red"
            MouseArea {
                anchors.fill: parent
                onPressed: child.color="gray"
                onReleased: child.color="red"
            }
        }
    }
}
4

2 回答 2

1

简单的方法:

Rectangle {
    id: top
    width: 100
    height: 32
    anchors.centerIn: parent
    color: "green"
    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onExited: top.color = "green"
        onEntered: top.color = "blue"
        Rectangle {
            width: 16
            height: 16
            anchors.centerIn: parent
            color: "red"
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onExited: top.color = "blue"
                onEntered: top.color = "green"
                onClicked: console.log("Clicked")
            }
        }
    }
}

你可以使用一些数学,

Rectangle {
    id: top
    width: 100
    height: 32
    anchors.centerIn: parent
    signal sgnEnteredZone()
    color: "green"
    onSgnEnteredZone:  {
        top.color = "blue"
    }
    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onMouseXChanged:{

            if( (mouseX <inRect.x ||mouseX>inRect.width + inRect.x )
                    && (mouseY <inRect.y ||mouseY>inRect.height + inRect.y)
                    )
                sgnEnteredZone()
            else
                top.color = "green"

        }
        onMouseYChanged:{
            if( (mouseX <inRect.x ||mouseX>inRect.width + inRect.x )
                    && (mouseY <inRect.y ||mouseY>inRect.height + inRect.y)
                    )
                sgnEnteredZone()
            else
                top.color = "green"
        }


        Rectangle {
            id:inRect
            width: 16
            height: 16
            anchors.centerIn: parent
            color: "red"
            MouseArea {
                anchors.fill: parent
                onClicked: console.log("Clicked")
            }
        }
    }
}
于 2015-08-07T19:07:48.027 回答
0

我发现只有在使用 Awesome 窗口管理器时才会出现这个问题。我不知道是什么原因造成的,但无论如何我现在将其标记为已解决。

于 2015-08-11T19:24:52.257 回答