在QML中,光标形状甚至在重叠的矩形上也发生了变化

时间:2013-05-16 21:49:00

标签: qt cursor qml

使用以下代码,绿色矩形与红色矩形完全重叠,但当鼠标位于(隐藏)红色矩形上方时,我的光标形状仍会根据红色MouseArea cursorShape进行更改。 有什么想法可以防止这种行为吗?

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360

    Rectangle {
        color: "red"
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        width: 100
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            cursorShape: "ClosedHandCursor"
        }
    }

    Rectangle {
        color: "green"
        anchors.fill: parent
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
        }
    }
}

2 个答案:

答案 0 :(得分:2)

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360

    Rectangle {
        color: "red"
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        width: 100
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            cursorShape: greenRectangle.hovered ? Qt.ArrowCursor : Qt.ClosedHandCursor;
        }
    }

    Rectangle {
        id: greenRectangle;

        property bool hovered: false;

        color: "green"
        anchors.fill: parent
        anchors.leftMargin: 20;
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onHoveredChanged: {
                greenRectangle.hovered = !greenRectangle.hovered;
                console.log(greenRectangle.hovered);
            }
        }
    }
}

答案 1 :(得分:2)

只需在cursorShape的绑定中使用'containsMouse'属性,并且不要使用枚举值的String形式:

import QtQuick 2.0

Rectangle {
    color: "white";
    width: 400;
    height: 400;

    Rectangle {
        color: "red";
        anchors.top: parent.top;
        anchors.left: parent.left;
        width: 300;
        height: 300;

        MouseArea {
            anchors.fill: parent;
            hoverEnabled: true;
            cursorShape: (containsMouse
                          ? (pressed
                             ? Qt.ClosedHandCursor
                             : Qt.OpenHandCursor)
                          : Qt.ArrowCursor);
        }
    }

    Rectangle {
        color: "green";
        anchors.bottom: parent.bottom;
        anchors.right: parent.right;
        width: 300;
        height: 300;

        MouseArea {
            anchors.fill: parent;
            hoverEnabled: true;
        }
    }
}