Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将onPositionChanged事件传播到QtQuick 1.1中的后台项_C++_Qt_Qml_Qt Quick_Qt4.8 - Fatal编程技术网

C++ 将onPositionChanged事件传播到QtQuick 1.1中的后台项

C++ 将onPositionChanged事件传播到QtQuick 1.1中的后台项,c++,qt,qml,qt-quick,qt4.8,C++,Qt,Qml,Qt Quick,Qt4.8,Background.qml import QtQuick 1.1 Item { MouseArea { id: backgroundMouseArea anchors.fill: parent hoverEnabled: true onPositionChanged: { console.log("Background") } } } import QtQuick 1.

Background.qml

import QtQuick 1.1

Item {
    MouseArea {
        id: backgroundMouseArea
        anchors.fill: parent
        hoverEnabled: true

        onPositionChanged: {
            console.log("Background")
        }
    }
}
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
        }
    }
}
前台.qml

import QtQuick 1.1

Item {
    MouseArea {
        id: backgroundMouseArea
        anchors.fill: parent
        hoverEnabled: true

        onPositionChanged: {
            console.log("Background")
        }
    }
}
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。对于按下的
on
,我会通过在前台项目中设置
mouse.accepted=false


我可以手动调用后台项目的
onPositionChanged
?如果是,我该怎么做?

我不完全确定您在这里想要实现什么。
MouseArea
用于从硬件获取鼠标事件。如果您真的想将鼠标事件从不同的
MouseArea
传播到后台,那么您实际想要做的可能是为
background
提供一个简单的
属性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}
    }
  }
}