From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


TableView QML problem when I add columns (all cells has the same value)



  • Hi,

    I'm using Qt 5.5.1 (64 bits) and I have a problem when I try add columns to one QML TableView.

    I wrote a little example with my problem.

    The program has a TableView and a button to add a new column, and fill the table with numbers.

    Columns are added correctly, but when I fill the rows, I always have the same value in all the cells.

    import QtQuick 2.3
    import QtQuick.Controls 1.2
    import QtQuick.Window 2.2
    
    ApplicationWindow {
        title: qsTr("TableView example")
        id: root
        width: 640
        height: 480
        visible: true
        signal addColum();
    
        onAddColum: {
            var n = table.columnCount
            addColumnToTable("C"+n)
            fillRow()
        }
    
        function addColumnToTable(name) {
            var columnString = 'import QtQuick 2.3; import QtQuick.Controls 1.2; TableViewColumn {role: "' + name + '"; title: "' + name + '"; }';
            var column = Qt.createQmlObject(columnString, table)
            if (!table.addColumn(column))
                console.log("Error column",name)
        }
    
        function fillRow() {
            if (!tableModel.count) {
                tableModel.append({"C0":50})
            }
            else {
                tableModel.setProperty(0,"C"+(table.columnCount-1),50+table.columnCount-1)
            }
            var x = tableModel.get(0)
            console.log("Num. Columns",table.columnCount,"row object",JSON.stringify(x))
        }
    
        ListModel {
            id: tableModel
        }
    
        TableView  {
            id: table
            anchors.fill: parent
            anchors.margins: 20
            anchors.bottomMargin: 50
            model: tableModel
        }
    
        Button {
            anchors.top: table.bottom
            anchors.topMargin: 5
            anchors.horizontalCenter: parent.horizontalCenter
            text: "Add"
            onClicked: addColum()
        }
    }
    

    This is the console output, that seems OK
    qml: Num. Columns 1 row object {"objectName":"","C0":50}
    qml: Num. Columns 2 row object {"objectName":"","C0":50,"C1":51}
    qml: Num. Columns 3 row object {"objectName":"","C0":50,"C1":51,"C2":52}
    qml: Num. Columns 4 row object {"objectName":"","C0":50,"C1":51,"C2":52,"C3":53}

    And The TableView is:
    C0 C1 C2 C3
    50 50 50 50

    Somebody can found the mistake??

    thanks



  • Finally I solved it!!!

        function fillRow() {
            table.model = undefined  // <--- 1
            if (!tableModel.count) {
                tableModel.append({"C0":50})
            }
            else {
                tableModel.setProperty(0,"C"+(table.columnCount-1),50+table.columnCount-1)
            }
            table.model = tableModel    //<--- 2
    
            var x = tableModel.get(0)
            console.log("Num. Columns",table.columnCount,"row object",JSON.stringify(x))
        }
    

    When I fill the columns, I set TableView.model to undefined(1), then fill the rows, and after I set TableView.model to ListModel again(2)


Log in to reply