Getting the Axis aligned Bound Box of an item is has transformations (rotation, scaling,...)



  • I need to find a way to retrieve the AABB of an item that works even if it has transformations applied.
    I thought anchors was always AA, but left can look to bottom with a rotation of -45 degrees.

    I need something precise to be able to place other elements correctly.

    I tried with a component but it's not really easy to implement and there is a lot of hacks without success.



  • I got the solution, I just miss Rect are Axis Aligned so we can simply apply the item transformation to get the AABB :

    AABB.qml
    @
    import QtQuick 2.1

    Rectangle {
    id: aabb
    color: "#AAAAFF"

    property real   childrenRotation: {
        if (children.length)
            return children[0].rotation
        else
            return false
    }
    
    property real   childrenScaling: {
        if (children.length)
            return children[0].scale
        else
            return false
    }
    
    onChildrenRotationChanged: update()
    onChildrenScalingChanged: update()
    
    function    update()
    {
        var rect = mapFromItem(children[0], children[0].x, children[0].y, children[0].width, children[0].height)
        width = rect.width
        height = rect.height
    }
    

    }
    @

    main.qml (Center the child in the AABB component) :
    @
    import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360
    AABB {
    anchors.centerIn: parent

        Text {
            text: qsTr("Hello World")
            anchors.centerIn: parent
            transformOrigin: Item.Center
    
            SequentialAnimation on rotation {
                // Animations on properties start running by default
                running: true
                loops: Animation.Infinite // The animation is set to loop indefinitely
                NumberAnimation { from: 0; to: 359; duration: 50000 }
            }
            SequentialAnimation on scale {
                // Animations on properties start running by default
                running: true
                loops: Animation.Infinite // The animation is set to loop indefinitely
                NumberAnimation { from: 0.25; to: 4; duration: 50000 }
            }
        }
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.