QML MouseArea重叠其他小部件

时间:2015-02-23 19:06:51

标签: qt qml qtquick2 qtquickcontrols

以下是一些简单的QML代码,其中包含ButtonMouseArea。我希望Button检测左右鼠标点击。

Rectangle {
    anchors.fill:parent;
    width: 1302
    height: 638

    Button {
        id: button1
        x: 378
        y: 332
        width: 194
        height: 66
        text: qsTr("Button")
    }

    MouseArea {
        id: mouseArea1
        x: 368
        y: 306
        width: 226
        height: 108
        acceptedButtons: Qt.RightButton
        propagateComposedEvents: true
        onClicked: {
            console.log("Click")
            mouse.accepted = false
        }
    }
}

由于MouseArea位于Button之上,如何强制Button接受鼠标事件?

1 个答案:

答案 0 :(得分:2)

如果我理解你,试试这个:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2


Window {
    id: screen
    width: 800
    height: 600
    visible: true

    Button {
        anchors.centerIn: parent
        text: qsTr("Button")
        onClicked: {
            console.log("Button was clicked");
        }
        z: 100
        MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.RightButton
                onClicked: {
                    console.log("Button right button was clicked");
                    mouse.accepted = true
                }
            }
    }

    MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.RightButton
            propagateComposedEvents: true
            onClicked: {
                console.log("Window right button was clicked")
            }
            z: 99
        }
}

但我建议你用常用的方式来显示按钮弹出窗口。使用Button.menu显示下拉菜单按钮。