我遇到了嵌套MouseArea
s 的问题。当我单击红色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"
}
}
}
}