在qtquick 1.1中传播定位的事件为背景项目



background.qml

import QtQuick 1.1
Item {
    MouseArea {
        id: backgroundMouseArea
        anchors.fill: parent
        hoverEnabled: true
        onPositionChanged: {
            console.log("Background")
        }
    }
}

foreground.qml

import QtQuick 1.1
Item {
    Background {
        width: 1920
        height: 1080
    }
    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        onPositionChanged: {
            console.log("Foreground")
            [mouse.accepted = false] - Not working (as the docs say)
            [backgroundMouseArea.onPositionChanged(mouse)] - Not working
        }
    }
}

我需要在背景和前景项目上执行onPositionChanged事件。

f.ex。对于onPressed,我会通过在前景项目中设置mouse.accepted = false来做到这一点。

我可以手动调用背景项目的onPositionChanged吗?如果是,我该怎么办?

我不完全确定您在这里要实现的目标。MouseArea旨在从硬件中获取鼠标事件。如果您真的想从不同的MouseArea传播鼠标事件到背景中,也许您实际想做的就是给Background一个简单的property mousePosition而不是MouseArea,然后从前景onPositionChanged处理程序设置该位置。

此外,您的前景代码依赖于背景内部的内部id参数。这真的很糟糕。考虑背景和前景"类"的"公共API"通常更有用。如果我上面描述的确实是您想做的,那么这就是IMHO的样子:

// Background.qml
import QtQuick 1.1
Rectangle {
    // an object with just x and y properties
    // or the complete mouseevent, whatever you want
    // Use variant for QtQuick 1/Qt4, var for QtQuick 2.0 / Qt5
    property variant mousePosition
    onMousePositionChanged: console.log(
      "Background " + mousePosition.x + " " + mousePosition.y
    )
}
//Foreground.qml
import QtQuick 1.1
Item {
  // use only ids defined in the same file
  // else, someone might change it and not know you use it
  Background { id: background }
  MouseArea {
    anchors.fill: parent
    hoverEnabled: true
    onPositionChanged: {
       console.log("Foreground")
       background.mousePosition = {x: mouse.x, y: mouse.y}
    }
  }
}

........................

最新更新