Nominate our 2022 Qt Champions!

Sizing a TableView to its content

  • Hi all,
    I'm trying to create a TableView that will always show all rows without the need for a scrollbar.

        id: compareTableView
        width: parent.width
        height: childrenRect.height
        model: myModel
        alternatingRowColors: true
        selectionMode: SelectionMode.NoSelection
        headerVisible: false
        frameVisible: false
        verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff
        flickableItem.interactive: false
        clip: true
        rowDelegate: Rectangle { height: 20 }
        TableViewColumn { ... }
        TableViewColumn { ... }
        TableViewColumn { ... }

    I tried to set the height of the TableView to childrenRect.height, which resulted in the TableView not being visible at all.
    Okay, so the size of the childrenRect isn't available at that time. So I tried to set the same in a Component.onCompleted-block. Now I do see the table, but it is only ~200 px high, which seems to be some sort of default height, since it is the same height I get when I omit a height alltogether.
    Then I thought: Maybe the 200 px height comes from the height of my rowDelegate multiplied by the rowCount. I changed its height to 50, but still got the same TableView height in return.

    Now I'm out of ideas, can anyone help me?