Search code examples
qtqmlgrid-layout

QML In a GridLayout, how to constraint a cell with an invisible to have a 0 height?


I've got a GridLayout laid out this way:

AAAA
C D
C E
C E
C E
C F
C G
HHHH

E has Layout.fillHeight: true, F has a preferredheight,
D and G can be hidden and have a preferredHeight set based on their content:

Item {
    Layout.row: 2
    Layout.column: 2            
    Layout.columnSpan: 1
    Layout.rowSpan: 1

    id : D
    visible : false
    Layout.preferredWidth : layConfig.implicitWidth + 10
    Layout.preferredHeight : layConfig.implicitHeight + 10
    anchors.margins : 20
    Grid {
        id : layConfig
        ...
        }

When D and G are visible they are sized correctly. But when they are invisible they are replaced by an empty space instead of a 0 height cell.

AAAA
C
C
C E
C E
C F
C G
HHHH

While this expected:

AAAA
C E
C E
C E
C E
C F
C G
HHHH

For now, I just added an extra ColumnLayout in the GridLayout for managing the elements D->G. In a shortterm it is working but on a longer term it will complicate the whole code.

How can I force, with a pure GridLayout, a 0 height in the case a cell is hidden?


Solution

  • Since you are setting the preferredHeight of the D cell already, you can extend the binding for that to include the visibility of the cell:

    Layout.preferredWidth : D.visible ? layConfig.implicitWidth + 10 : 0
    

    As you are not showing the rest of your code, it is possible some additional tweaking is needed, but this should get a step further. For example, the rowSpan might have to be adjusted for C, and maybe you are using Layout.row?