qtqmlqt-quick

Qt quick layout which fills parent window and has margins


I am trying to use a Column layout which fills the ApplicationWindow parent. I want the window to have content padding, so I guess I have to put margin to the layout. This is the approach I am taking:

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

ApplicationWindow {
    id: root

    width: rootLayout.implicitWidth
    height: rootLayout.implicitHeight
    minimumWidth: rootLayout.Layout.minimumWidth
    minimumHeight: rootLayout.Layout.minimumHeight

    ColumnLayout {
        id: rootLayout
        anchors.margins: 10
        anchors.fill: parent
   }
}

But it does not work fine since the minimum height property of the layout does not take into account the margins and the window can shrink more than it should be possible.


Solution

  • import QtQuick
    import QtQuick.Controls
    import QtQuick.Layouts
    
    ApplicationWindow {
        id: root
    
        visible: true
    
        width: minimumWidth
        height: minimumHeight
    
        minimumWidth: rootLayout.implicitWidth + rootLayout.anchors.margins*2
        minimumHeight: rootLayout.implicitHeight + rootLayout.anchors.margins*2
    
        ColumnLayout {
            id: rootLayout
            anchors.fill: parent
            anchors.margins: 10
       }
    }
    

    Also note, that you must also fill ColumnLayout with some real elements with correctly defined implicit sizes for the everything to be sized properly.