layoutqmlpositioning

Positioning the Items using Layouts in qml


I started learning about Layouts and i am trying to position the elements using Layouts (ColumnLayout & RowLayout) but unable to position the rectangle (green one with Width&Height 50) on to the right side of the screen, whatever i my do i can't bring it to the right side if i am using RowLayout inside a ColumnLayout.

Expected position of Green rectangle enter image description here

Any help here will be appreciated

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3

Window {
    visible: true
    width: 800
    height: 480
    title: qsTr("Hello World")
    ColumnLayout{
        anchors.fill: parent
        spacing: 10

        RowLayout{

            //implicitWidth: parent.width
            //implicitHeight: parent.height
            Layout.fillWidth: true
            Layout.fillHeight: true

            Rectangle{
                width: 50
                height: 50

                color: "orange"
            }

            Rectangle{
                width: 50
                height: 50

                color: "blue"
            }

            Rectangle{
                width: 50
                height: 50

                color: "green"
                Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
            }
        }
        RowLayout {
            Rectangle{
                width: 370
                height: 800

                color: "cyan"
            }

            Rectangle{
                width: 370
                height: 800

                color: "pink"
            }
        }
    }
}

Regard's,

Rohith.G


Solution

  • You can add an empty Item as a placeholder:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.3
    
    Window {
    visible: true
    width: 800
    height: 480
    title: qsTr("Hello World")
    ColumnLayout{
        anchors.fill: parent
        spacing: 10
    
        RowLayout{
    
            //implicitWidth: parent.width
            //implicitHeight: parent.height
            Layout.fillWidth: true
            Layout.fillHeight: true
    
            Rectangle{
                width: 50
                height: 50
    
                color: "orange"
            }
    
            Rectangle{
                width: 50
                height: 50
    
                color: "blue"
            }
    
            // placeholder item taking the remaining space
            Item {
                Layout.fillWidth: true
            }
    
            Rectangle{
                width: 50
                height: 50
    
                color: "green"
                Layout.alignment: Qt.AlignVCenter | Qt.AlignRight
            }
        }
        RowLayout {
            Rectangle{
                width: 370
                height: 800
    
                color: "cyan"
            }
    
            Rectangle{
                width: 370
                height: 800
    
                color: "pink"
            }
        }
    }
    }