将鼠标事件发送到QML对象

时间:2012-01-04 02:03:17

标签: mouse qml

我需要从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

1 个答案:

答案 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);
                       }
               }
       }
}