Background.qml
import QtQuick 1.1
Item {
MouseArea {
id: backgroundMouseArea
anchors.fill: parent
hoverEnabled: true
onPositionChanged: {
console.log("Background")
}
}
}
Foreground.qml
import QtQuick 1.1
Item {
Background {
width: 1920
height: 1080
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
onPositionChanged: {
console.log("Foreground")
[mouse.accepted = false] - Not working (as the docs say)
[backgroundMouseArea.onPositionChanged(mouse)] - Not working
}
}
}
我需要在后台和前台项目上执行onPositionChanged
事件。
F.ex。对于onPressed
,我会通过在前景项中设置mouse.accepted = false
来实现。
我可以手动调用后台项目的onPositionChanged
吗?如果是,我该怎么做?
答案 0 :(得分:1)
我不完全确定你在这里想要实现的目标。
MouseArea
旨在从硬件中获取鼠标事件。如果你真的想要将鼠标事件从不同的MouseArea
传播到背景,那么你真正想做的就是给Background
一个简单的property mousePosition
而不是MouseArea
,并且然后从Foreground onPositionChanged
处理程序设置该位置。
此外,您的Foreground代码依赖于Background中的内部id
参数。这闻起来很糟糕。考虑背景和前景“类”的“公共API”通常更有用。如果我上面描述的是你真正想做的事情,那就是它应该看起来像恕我直言:
// Background.qml
import QtQuick 1.1
Rectangle {
// an object with just x and y properties
// or the complete mouseevent, whatever you want
// Use variant for QtQuick 1/Qt4, var for QtQuick 2.0 / Qt5
property variant mousePosition
onMousePositionChanged: console.log(
"Background " + mousePosition.x + " " + mousePosition.y
)
}
//Foreground.qml
import QtQuick 1.1
Item {
// use only ids defined in the same file
// else, someone might change it and not know you use it
Background { id: background }
MouseArea {
anchors.fill: parent
hoverEnabled: true
onPositionChanged: {
console.log("Foreground")
background.mousePosition = {x: mouse.x, y: mouse.y}
}
}
}
...........