【问题标题】:Propagating onPositionChanged events to background items in QtQuick 1.1将 onPositionChanged 事件传播到 QtQuick 1.1 中的背景项目
【发布时间】:2017-03-10 10:45:23
【问题描述】:

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吗?如果是,我该怎么做?

【问题讨论】:

    标签: c++ qt qml qt-quick qt4.8


    【解决方案1】:

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

    此外,您的前台代码依赖于后台内部的内部id 参数。这闻起来真的很糟糕。考虑背景和前景“类”的“公共 API”通常更有用。如果我上面描述的确实是你想要做的,这就是它应该看起来像恕我直言:

    // 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}
        }
      }
    }
    

    ...........

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多