发送鼠标事件到QML对象

5
我需要在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
  }
}

我希望标记为“HERE”的行能够触发rect的点击事件,并将事件传递给MouseArea
1个回答

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

1
看起来childAt()函数是我这种情况的正确解决方案。谢谢! - Dan
5
链接已失效。也许需要添加一些信息? - Mr. Developerdude
我如何向TextArea组件发送事件单击,以便它接收焦点并将光标放置在与单击位置相对应的位置?上面显示的方法不起作用,因为TextArea的typeof(idArea [eventType]):未定义。 - pier_nasos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接