I need to send a Mouse event to a QML object, from QML. For example,
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
}
}
I'd like the line marked "HERE" to cause a click event for rect
which would be passed down to the MouseArea
.
There seems to be some relation between your question and this question
Please take a look.
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);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With