Search code examples
qtqmlqt5qtquick2qtquickcontrols2

Spacer Item in QML Layouts


I want to create a layout in QML and I'd like to add a spacer item (the bottom selected item from the image below) just as you do using widgets like so:

enter image description here

But I couldn't find anything to suit this on the QtQuick side of things...is it possible to have this kind of layout in QML w/o using the anchoring system? I'd prefer the layouts approach...


Solution

  • You can simply use an Item with Layout.fillHeight: true :

    import QtQuick 2.0
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.3
    
    ApplicationWindow {
        visible: true
        ColumnLayout {
            anchors.fill: parent
            Button {
                Layout.fillWidth: true
                text: "PushButton"
            }
            Button {
                Layout.fillWidth: true
                text: "PushButton"
            }
            Label {
                Layout.fillWidth: true
                text: "TextLabel"
            }
            Item {
                // spacer item
                Layout.fillWidth: true
                Layout.fillHeight: true
                Rectangle { anchors.fill: parent; color: "#ffaaaa" } // to visualize the spacer
            }
        }
    }
    

    EDIT: Alternatively here, you could have used a Column with no spacer item since a Column just positions its children from top to bottom and don't spread them to take all the available space.