qtqmlqtquick2qtquickcontrols2qtquickcontrols

TableView replacement for TableViewColumn in Qt Quick 2


In Qt Quick Controls 1 we can style different columns from a model with TableViewColumn:

TableView {

    anchors.fill: parent

    TableViewColumn {title: "1"; role: "one"; width: 70 }
    TableViewColumn {title: "2"; role: "two"; width: 70   }
    TableViewColumn {title: "3"; role: "three"; width: 70 }

    model: theModel

}

How can we achieve a similar result in Qt Quick 2 if there is no more TableViewColumn for the TableView?


Solution

  • Since Qt 5.12 you can use TableView QML type. But to have everything you want you need to include Qt.labs.qmlmodels. It all is available in Qt 5.15 (use online installer).

    Actual implementation will hardly depend on your requirements, but here is an example of how it could be done.

    rows: [
            // Each property is one cell/column.
            {
              checked: false,
              amount: 1,
              type: "Apple",
              price: 1.50
            },
            {
              checked: true,
              amount: 4,
              type: "Orange",
              price: 2.50
            },
            ...
          ]
    
    DelegateChoice {
      column: 1
      delegate: ProgressBar {
        enabled: false
        width: 100
        from: 0.0
        to: 10.0
        value: model.display
      }
    }
    

    As result you could easily get this application entirely in QML (whiteout need to use C++ and/or old QtQuick.Controls):

    Example of application

    Please refer to this repository to get full application.