首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套MouseArea悬停

嵌套MouseArea悬停
EN

Stack Overflow用户
提问于 2015-08-07 17:50:17
回答 2查看 1.3K关注 0票数 2

我遇到了嵌套MouseArea的问题。当我单击红色Rectangle时,它会在Rectangle顶部触发exited事件(至少对我来说是这样)。这是故意的行为吗?如果是的话,有解决办法吗?

编辑

澄清

我想要的是能够在单击子Rectangle时保持顶部Rectangle的悬停(顶部Rectangle应该保持blue)。这是有用的,如果我想隐藏和取消隐藏按钮等悬停,这是web接口(HTML/CSS)中的常见做法。现在这样做会产生奇怪的效果,因为单击entered信号显示的按钮将在单击它们时消失。

编辑

我用想要的行为做了一个Javascript演示:https://jsfiddle.net/uo4vousu/,即使您单击红色的矩形,父框仍然是蓝色的,这就是我想要的。在QML示例中,父级变为绿色。我更新了QML代码以处理jsfiddle。

编辑

这可能是linux唯一的问题。

下面是一个例子:

代码语言:javascript
复制
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"
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-11 19:24:52

我发现这个问题只有在使用可怕的窗口管理器时才会出现。我不知道是什么原因造成的,但我认为这件事目前已经解决了。

票数 0
EN

Stack Overflow用户

发布于 2015-08-07 19:07:48

简单的方法:

代码语言:javascript
复制
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")
            }
        }
    }
}

你可以用一些数学,

代码语言:javascript
复制
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")
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31883830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档