无法在QML中处理子项直接鼠标事件

时间:2015-09-22 08:53:03

标签: qt qml qt-quick

我上面有一个父矩形,有一个子矩形,两个矩形都有鼠标事件,但子矩形没有任何鼠标事件,总是父矩形处理。

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

问题:

无法处理子矩形鼠标事件

1 个答案:

答案 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")
        }
    }
相关问题