将 onPositionChanged 事件传播到 QtQuick 1.1 中的背景项
Propagating onPositionChanged events to background items in QtQuick 1.1
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
吗?如果是,我该怎么做?
我不完全确定你想在这里实现什么。
MouseArea
用于从硬件中获取鼠标事件。如果你真的想将鼠标事件从不同的 MouseArea
传播到背景,也许你真正想要做的是给 Background
一个简单的 property mousePosition
而不是 MouseArea
,并且然后从前景 onPositionChanged
处理程序设置该位置。
此外,您的前台代码依赖于后台内部的 id
参数。这闻起来真的很糟糕。往往想想Background和Foreground"classes"的"Public 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}
}
}
}
............
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
吗?如果是,我该怎么做?
我不完全确定你想在这里实现什么。
MouseArea
用于从硬件中获取鼠标事件。如果你真的想将鼠标事件从不同的 MouseArea
传播到背景,也许你真正想要做的是给 Background
一个简单的 property mousePosition
而不是 MouseArea
,并且然后从前景 onPositionChanged
处理程序设置该位置。
此外,您的前台代码依赖于后台内部的 id
参数。这闻起来真的很糟糕。往往想想Background和Foreground"classes"的"Public 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}
}
}
}
............