简体   繁体   English

QML中的MouseAreas重叠

[英]MouseAreas overlapping in QML

How can I get MouseAreas, from rectangles that I've put on front and back of Flipable item, work? 如何从我在Flipable物品的前面和后面放置的矩形中获取MouseAreas起作用?

Whenever I click Flipable item, it flips, but when I click on image that's supposed to do other thing, not flip, it flips again. 每当我单击“可翻转项目”时,它就会翻转,但是当我单击应该做其他事情的图像而不是翻转时,它会再次翻转。

So, I need to get these MouseAreas in Images work when it's clicked on them, and not to flip item as it does now. 因此,我需要在单击它们时使它们在Images中起作用,而不是像现在那样翻转项。

Here is my code, any help would be very appreciated. 这是我的代码,任何帮助将不胜感激。

import QtQuick 2.0

Item {
    x: 400
    Flipable {
        id: flipable
        width: 400
        height: 500
        property bool flipped: false

        front: Rectangle {
            id: page1
            width: 400
            height: 500
            color: "transparent"
            Image //this is the background of Flipable front
            {
                id: bookright
                source: "r.png"
            }
            Image //this is image that works like button
            {
                x: 30
                y: 130
                id: front
                source: "BTest.png"

                MouseArea {
                    anchors.fill: front
                    onClicked: {
                        Logic.onClicked(2)
                    }
                }
            }
        }

        back: Rectangle {
            id: page2
            width: 400
            height: 500
            color: "transparent"

            Image {
                id: bookleft
                source: "l.png"
            }
            Image {
                x: 30
                y: 130
                id: back
                source: "Zucenje.png"

                MouseArea {
                    anchors.fill: back
                    onClicked: {
                        Logic.onClicked(3)
                    }
                }
            }
        }

        transform: Rotation {
            id: rotation
            origin.x: 0
            origin.y: 0
            axis.x: 0
            axis.y: 1
            axis.z: 0 // set axis.y to 1 to rotate around y-axis
            angle: 0 // the default angle
        }

        states: State {
            name: "back"
            PropertyChanges {
                target: rotation
                angle: -180
            }
            when: flipable.flipped
        }

        transitions: Transition {
            NumberAnimation {
                target: rotation
                property: "angle"
                duration: 2000
            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: flipable.flipped = !flipable.flipped
        }
    }
}

All the MouseArea s in your code are roughly at the same hierarchical level, so the one which will receive the clicks is ambiguous [*]. 您代码中的所有MouseArea大致处于相同的层次结构级别,因此将获得点击的那个是模糊的[*]。

You can solve this by dynamically changing the z-order of the item which should catch the mouse events: 您可以通过动态更改应捕获鼠标事件的项目的z顺序来解决此问题:

front: Rectangle {
    z: flipable.flipped ? -1 : 1  // on top when not flipped
...
back: Rectangle {
    z: flipable.flipped ? 1 : -1  // on top when flipped

[*]: Actually, the last MouseArea is "drawn" above the others because it appears last in the code according to the rules in the documentation . [*]:实际上,最后一个MouseArea被“绘制”在其他之上,因为它根据文档中规则出现在代码的最后。 But the front item also seems to be logically "drawn" above the back item even when it is not visible, so, the z property has to be adjusted when sides are flipped. 但是,即使看不见前项,也似乎在逻辑上“绘制”在了后项上方,因此,在翻转侧面时必须调整z属性。

Check the value of Flipable's side property before flipping it. 翻转之前,请检查Flipable的side属性的值。 Working off the example from the documentation: 根据文档处理示例:

import QtQuick 2.0

Flipable {
    id: flipable
    width: 240
    height: 240

    property bool flipped: false

    front: Rectangle {
        color: "red"
        anchors.fill: parent

        Text {
            text: "Front"
            anchors.centerIn: parent
        }
    }

    back: Rectangle {
        color: "blue"
        anchors.fill: parent

        Text {
            text: "Back"
            anchors.centerIn: parent
        }
    }

    transform: Rotation {
        id: rotation
        origin.x: flipable.width/2
        origin.y: flipable.height/2
        axis.x: 0
        axis.y: 1
        axis.z: 0
        angle: 0
    }

    states: State {
        name: "back"
        PropertyChanges {
            target: rotation;
            angle: 180
        }
        when: flipable.flipped
    }

    transitions: Transition {
        NumberAnimation {
            target: rotation;
            property: "angle";
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            if (flipable.side == Flipable.Front)
                flipable.flipped = true
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM