Krz*_*ski 5 qt cursor qml qt5 qtquick2
有两个负责两个不同职责的MouseArea,一个(绿色)部分位于另一个(红色)上方。每当悬停红色 MA(即使它在绿色 MA下)时,我都想更改光标的形状,并且我希望绿色 MA对按下做出反应,而没有其他事情。
两个MA可能位于不同的文件中,所以我不想在它们之间建立明确的依赖关系,例如,每当红色更改containsMouse时,就将绿色设置为正确的cursorShape 。有没有办法防止绿色的 MouseArea处理光标形状?
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
visible: true
width: 200
height: 200
Rectangle { anchors.fill: parent; color: "yellow"; }
MouseArea {
width: 150
height: 150
hoverEnabled: true
cursorShape: Qt.OpenHandCursor
Rectangle { anchors.fill: parent; color: "red"; }
onPositionChanged: console.log("position", mouse.x, mouse.y)
onContainsMouseChanged: console.log("containsMouse", containsMouse)
}
MouseArea {
x: 50
y: 50
width: 150
height: 150
hoverEnabled: false
Rectangle { anchors.fill: parent; color: "green"; }
onPressed: console.log("Ahoj!")
}
}
Run Code Online (Sandbox Code Playgroud)
没有办法通过MouseArea属性或任何其他现成的解决方案来做到这一点。MouseArea始终设置一些光标形状——如果未指定cursorShape属性,则使用默认值 ( Qt.ArrowCursor) 您当然可以使用mapToItem()/mapFromItem()来解决此问题(如 Mitch 建议的那样)。但也有其他可能性:
您可以临时更改visible为false覆盖鼠标区域。
或者,如果两者MouseArea都是兄弟,您可以对z属性进行操作以获得适合您需要的特定对象层次结构。