Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Center all items in a Column

    QML and Qt Quick
    2
    4
    2585
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • X
      xargs1 last edited by

      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.

      vishnu 1 Reply Last reply Reply Quote 0
      • vishnu
        vishnu @xargs1 last edited by

        @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"
                       }
        }
        
        1 Reply Last reply Reply Quote 0
        • X
          xargs1 last edited by

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

          1 Reply Last reply Reply Quote 0
          • X
            xargs1 last edited by

            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;
                }
            }
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post