Wrong coordinates white getting real position of item relative to its parent

后端 未结 2 1546
逝去的感伤
逝去的感伤 2021-01-19 18:50

I have simple scene with only 2 Rectangles. The difference is that first one uses absolute coordinates and second one uses anchors. In this case bo

2条回答
  •  醉酒成梦
    2021-01-19 19:11

    Both rectangles have same coordinates but on different time:

    import QtQuick 2.4
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 600
        height: 600
        Rectangle {
            id: rec1
            x: 200
            y: 200
            width: 200
            height: 200
            color: "green"
            opacity: 0.5
            Component.onCompleted: console.log("rec1: " + rec1.x + "," + rec1.y);
        }
    
        Rectangle {
            id: rec2
            anchors.centerIn: parent
            width: 200
            height: 200
            color: "blue"
            opacity: 0.5
            Component.onCompleted: console.log("rec2: " + rec2.x + "," + rec2.y);
            onXChanged: console.log("rec2.x: " + rec2.x);
            onYChanged: console.log("rec2.y: " + rec2.y);
        }
    }
    

提交回复
热议问题