Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. TableView QML problem when I add columns (all cells has the same value)

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
tableviewcolumns
2 Posts 1 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roigallego
    wrote on 11 Feb 2016, 06:17 last edited by roigallego 2 Nov 2016, 07:34
    #1

    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

    1 Reply Last reply
    0
    • R Offline
      R Offline
      roigallego
      wrote on 24 Feb 2016, 06:30 last edited by
      #2

      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)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved