我上面有一个父矩形,有一个子矩形,两个矩形都有鼠标事件,但子矩形没有任何鼠标事件,总是父矩形处理。
main.qml
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
width: 500
height: 500
title: qsTr("Hello World")
Rectangle{
id: outerrect
color: "green"
anchors.fill: parent
Rectangle{
id: innerrect
width: 100
height: 100
color: "lightblue"
anchors.centerIn: parent
MouseArea{
anchors.fill: parent
hoverEnabled: true
onClicked: {
console.log("child")
}
}
}
MouseArea{
anchors.fill: parent
hoverEnabled: true
onClicked: {
console.log("parent")
}
}
}
}
问题:
无法处理子矩形鼠标事件
答案 0 :(得分:2)
请参阅属性propagateComposedEvents
of MouseArea QML Type
因此,如果您只想通过子矩形处理单击,则可以更改父级{child} MouseArea
块中Rectangle
块的顺序。它改变了按块处理事件的顺序。
要激活两个处理程序,顶部对象应具有propagateComposedEvents: true
属性,并且mouse.accepted = false
处理程序中也应设置onClicked
,例如:
MouseArea{
anchors.fill: parent
propagateComposedEvents: true
hoverEnabled: true
onClicked: {
mouse.accepted = false
console.log("parent")
}
}