2

使用以下代码,绿色矩形与红色矩形完全重叠,但是当鼠标悬停在(隐藏的)红色矩形上时,我的光标形状仍然根据红色 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
        }
    }
}
4

2 回答 2

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);
            }
        }
    }
}
于 2013-05-28T07:09:26.080 回答
2

只需在 cursorShape 的绑定中使用 'containsMouse' 属性,不要使用枚举值的字符串形式:

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;
        }
    }
}
于 2013-05-28T10:49:01.750 回答