我需要从QML向QML对象发送一个Mouse事件。例如,
Rectangle
{
id: rect
MouseArea
{
anchors.fill: parent
onClicked: console.log(mouse.x + ', ' + mouse.y)
}
Rectangle
{
x: 0; y: 0; width: 50; height: 50
color: 'red'
onClicked: rect.click(randomX(), randomY()) // <---- HERE
}
}
我希望标有“此处”的行能够导致rect
的点击事件,该事件将传递给MouseArea
。
答案 0 :(得分:3)
您的问题与this question
之间似乎存在某种关系请看一下。
import QtQuick 1.0
Rectangle {
width: 360
height: 360
MouseArea {
anchors {fill: parent; margins: 40}
onClicked: console.log("hello from below");
}
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: {
console.log("hello from top")
forwardEvent(mouse, "clicked");
}
function forwardEvent(event, eventType) {
mouseArea.visible = false
var item = parent.childAt(event.x, event.y)
mouseArea.visible = true
if (item && item != mouseArea && typeof(item[eventType]) == "function") {
item[eventType](event);
}
}
}
}