我需要将鼠标事件从QML发送到QML对象。例如,
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
}
}
我希望标记为“ HERE”的行引起
rect
的点击事件,该事件将传递到MouseArea
。 最佳答案
您的问题和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);
}
}
}
}