C++ 将onPositionChanged事件传播到QtQuick 1.1中的后台项
Background.qmlC++ 将onPositionChanged事件传播到QtQuick 1.1中的后台项,c++,qt,qml,qt-quick,qt4.8,C++,Qt,Qml,Qt Quick,Qt4.8,Background.qml import QtQuick 1.1 Item { MouseArea { id: backgroundMouseArea anchors.fill: parent hoverEnabled: true onPositionChanged: { console.log("Background") } } } import QtQuick 1.
import QtQuick 1.1
Item {
MouseArea {
id: backgroundMouseArea
anchors.fill: parent
hoverEnabled: true
onPositionChanged: {
console.log("Background")
}
}
}
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
}
}
}
前台.qml
import QtQuick 1.1
Item {
MouseArea {
id: backgroundMouseArea
anchors.fill: parent
hoverEnabled: true
onPositionChanged: {
console.log("Background")
}
}
}
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。对于按下的on
,我会通过在前台项目中设置mouse.accepted=false
我可以手动调用后台项目的
onPositionChanged
?如果是,我该怎么做?我不完全确定您在这里想要实现什么。
MouseArea
用于从硬件获取鼠标事件。如果您真的想将鼠标事件从不同的MouseArea
传播到后台,那么您实际想要做的可能是为background
提供一个简单的属性mousePosition
,而不是MouseArea
,然后从前台onPositionChanged
处理程序设置该位置
此外,您的前台代码依赖于后台内部的id
参数。这闻起来真难闻。考虑后台和前台“类”的“公共API”通常更有用。如果我上面描述的是您真正想要做的事情,那么这就是它应该是什么样子的IMHO:
// 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}
}
}
}