Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to find distance from one item to other



  • Hi, I have a green rectangle, i want to find the distance from green rectangle bottom to red rectangle bottom.

    Rectangle
    {
        id: rec
        height: 200
        width: 200
        color: "red"
    
        Rectangle
        {
            width: 20
            height: 20
            color: "green"
            x:20
            y:40
        }
    }
    
    

    0_1551089057014_distance.png



  • @Bhushan_Sure

    Use the Y position and height to calculate the distance.
    Updated your code snippet with calculation.

    Rectangle
        {
            id: rectRed
            height: 200
            width: 200
            color: "red"
    
            Text {
                id: distance
                anchors.centerIn: parent
                text: "distance = " + ((rectRed.y + rectRed.height) - (rectGreen.y + rectGreen.height))
            }
    
            Rectangle
            {
                id: rectGreen
                width: 20
                height: 20
                color: "green"
                x:20
                y:40
            }
        }
    

    I hope this si the same you are seeking for.



  • @Mammamia Thank you very much :)


Log in to reply