将鼠标事件发送到 QML 对象

Dan*_*Dan 5 mouse qml

我需要从 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
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望标记为“HERE”的行引发一个单击事件,rect该事件将被传递到MouseArea.

Raj*_*rma 4

你的问题和这个问题之间似乎有某种关系

请看一下。

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);
                       }
               }
       }
}
Run Code Online (Sandbox Code Playgroud)

  • 链接已失效。也许添加一些信息? (5认同)