Changing TableView columnWidthProvider from 0 (hidden) to non-zero

  • I have a TableView inside my Window. I am using columnWidthProvider to provide the column width to be a specific size, and I am calling onWidthChanged: forceLayout() to force updates after width changes. However if the initial value returned by columnWidthProvider is zero (meaning telling TableView to hide that column) then the update does not have any effect. Is this a bug in TableView? If not how can I circumvent this issue? I think if not a bug, this would be incompatible with design of other QtQuick components such as for example the Window that has the TreeView inside, since it first creates its contentItem at size 0x0 and then resizes it to the final Window size. Therefore the TableView would only behave properly if I set a fake initial size for it if used inside a Window (which I prefer not to do).

    Example code:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import Qt.labs.qmlmodels 1.0
    Window {
        width: 400
        height: 400
        visible: true
        TableView {
            anchors.fill: parent
            columnSpacing: 1
            rowSpacing: 1
            boundsBehavior: Flickable.StopAtBounds
    		/* does not resize and have the column appear */
    		columnWidthProvider: function (column) { return column === 0 ? width : 0 }
    		onWidthChanged: forceLayout()
            model: TableModel {
                TableModelColumn { display: "checked" }
                TableModelColumn { display: "amount" }
                TableModelColumn { display: "fruitType" }
                TableModelColumn { display: "fruitName" }
                TableModelColumn { display: "fruitPrice" }
                // Each row is one type of fruit that can be ordered
                rows: [
                        // Each property is one cell/column.
                        checked: false,
                        amount: 1,
                        fruitType: "Apple",
                        fruitName: "Granny Smith",
                        fruitPrice: 1.50
                        checked: true,
                        amount: 4,
                        fruitType: "Orange",
                        fruitName: "Navel",
                        fruitPrice: 2.50
                        checked: false,
                        amount: 1,
                        fruitType: "Banana",
                        fruitName: "Cavendish",
                        fruitPrice: 3.50
            delegate:  TextInput {
                text: model.display
                padding: 12
                selectByMouse: true
                onAccepted: model.display = text
                Rectangle {
                    anchors.fill: parent
                    color: "#efefef"
                    z: -1

  • This post is deleted!

Log in to reply