Center all items in a Column



  • Is there an easy way to horizontally center everything in a Column? I know you can apply anchors.horizontalCenter: parent.horizontalCenter to every item in the column, but this is tedious when there are a lot of items in the column (as well as not maintainable if you change your mind about the layout later). ColumnLayout doesn't help, because you'd have to add Layout.alignment: Qt.AlignCenter to every item.



  • @xargs1
    Can you try like this by just adding anchors.horizontalCenter: parent.horizontalCenter to the column element but not to the child elements.

          Column{
                id:col
                anchors.horizontalCenter: parent.horizontalCenter
                 Button{
                    id:openDetailedview1
                    text:"View1"
                   }
                 Button{
                    id:openDetailedview2
                    text:" View2"
                   }
                 Button{
                    id:openDetailedview3
                    text:"View3"
                   }
    }
    


  • That centers the column itself, not the children within the column.



  • I've created a new element that will do what I want:

    // CenteredColumn.qml:

    import QtQuick 2.0
    
    Column {
        id: column
    
        Component.onCompleted: {
            for (var item in children)
                children[item].anchors.horizontalCenter = column.horizontalCenter;
        }
    }
    

Log in to reply
 

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