Send a Mouse event to a QML object

I need to send a Mouse event to a QML object starting with QML. For instance,

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 need a line that says HERE to trigger the click event for rect , which will be passed to MouseArea .

+4
source share
1 answer

There seems to be some kind of connection 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); } } } } 
+3
source

Source: https://habr.com/ru/post/1389188/


All Articles