Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Mouse event to QML object

Tags:

mouse

qml

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.

like image 633
Dan Avatar asked Oct 10 '22 02:10

Dan


1 Answers

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);
                       }
               }
       }
}
like image 179
RajaRaviVarma Avatar answered Oct 13 '22 11:10

RajaRaviVarma