TableModel QML Type

Encapsulates a simple table model. More...

Import Statement: import Qt.labs.qmlmodels 1.0
Since: Qt 5.14

Properties

Methods

  • appendRow(object row)
  • clear()
  • variant data(QModelIndex index, string role)
  • object getRow(int rowIndex)
  • QModelIndex index(int row, int column)
  • insertRow(int rowIndex, object row)
  • moveRow(int fromRowIndex, int toRowIndex, int rows)
  • removeRow(int rowIndex, int rows)
  • bool setData(QModelIndex index, string role, variant value)
  • setRow(int rowIndex, object row)

Detailed Description

The TableModel type stores JavaScript/JSON objects as data for a table model that can be used with TableView. It is intended to support very simple models without requiring the creation of a custom QAbstractTableModel subclass in C++.

import QtQuick 2.12
import QtQuick.Window 2.12
import Qt.labs.qmlmodels 1.0

Window {
    width: 400
    height: 400
    visible: true

    TableView {
        anchors.fill: parent
        columnSpacing: 1
        rowSpacing: 1
        boundsBehavior: Flickable.StopAtBounds

        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
            }
        }
    }
}

The model's initial row data is set with either the rows property or by calling appendRow(). Each column in the model is specified by declaring a TableModelColumn instance, where the order of each instance determines its column index. Once the model's Component::completed() signal has been emitted, the columns and roles will have been established and are then fixed for the lifetime of the model.

To access a specific row, the getRow() function can be used. It's also possible to access the model's JavaScript data directly via the rows property, but it is not possible to modify the model data this way.

To add new rows, use appendRow() and insertRow(). To modify existing rows, use setRow(), moveRow(), removeRow(), and clear().

It is also possible to modify the model's data via the delegate, as shown in the example above:

        delegate:  TextInput {
            text: model.display
            padding: 12
            selectByMouse: true

            onAccepted: model.display = text

            Rectangle {
                anchors.fill: parent
                color: "#efefef"
                z: -1
            }
        }

If the type of the data at the modified role does not match the type of the data that is set, it will be automatically converted via QVariant.

Supported Row Data Structures

TableModel is designed to work with JavaScript/JSON data, where each row is a simple key-pair object:

{
    // Each property is one cell/column.
    checked: false,
    amount: 1,
    fruitType: "Apple",
    fruitName: "Granny Smith",
    fruitPrice: 1.50
},
// ...

As model manipulation in Qt is done via row and column indices, and because object keys are unordered, each column must be specified via TableModelColumn. This allows mapping Qt's built-in roles to any property in each row object.

Complex row structures are supported, but with limited functionality. As TableModel has no way of knowing how each row is structured, it cannot manipulate it. As a consequence of this, the copy of the model data that TableModel has stored in rows is not kept in sync with the source data that was set in QML. For these reasons, TableModel relies on the user to handle simple data manipulation.

For example, suppose you wanted to have several roles per column. One way of doing this is to use a data source where each row is an array and each cell is an object. To use this data source with TableModel, define a getter and setter:

TableModel {
    TableModelColumn {
        display: function(modelIndex) { return rows[modelIndex.row][0].checked }
        setDisplay: function(modelIndex, cellData) { rows[modelIndex.row][0].checked = cellData }
    }
    // ...

    rows: [
        [
            { checked: false, checkable: true },
            { amount: 1 },
            { fruitType: "Apple" },
            { fruitName: "Granny Smith" },
            { fruitPrice: 1.50 }
        ]
        // ...
    ]
}

The row above is one example of a complex row.

Note: Row manipulation functions such as appendRow(), removeRow(), etc. are not supported when using complex rows.

Using DelegateChooser with TableModel

For most real world use cases, it is recommended to use DelegateChooser as the delegate of a TableView that uses TableModel. This allows you to use specific roles in the relevant delegates. For example, the snippet above can be rewritten to use DelegateChooser like so:

import QtQuick 2.12
import QtQuick.Controls 2.5
import Qt.labs.qmlmodels 1.0

ApplicationWindow {
    width: 400
    height: 400
    visible: true

    TableView {
        anchors.fill: parent
        columnSpacing: 1
        rowSpacing: 1
        boundsBehavior: Flickable.StopAtBounds

        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: DelegateChooser {
            DelegateChoice {
                column: 0
                delegate: CheckBox {
                    checked: model.display
                    onToggled: model.display = checked
                }
            }
            DelegateChoice {
                column: 1
                delegate: SpinBox {
                    value: model.display
                    onValueModified: model.display = value
                }
            }
            DelegateChoice {
                delegate: TextField {
                    text: model.display
                    selectByMouse: true
                    implicitWidth: 140
                    onAccepted: model.display = text
                }
            }
        }
    }
}

The most specific delegates are declared first: the columns at index 0 and 1 have bool and integer data types, so they use a CheckBox and SpinBox, respectively. The remaining columns can simply use a TextField, and so that delegate is declared last as a fallback.

See also TableModelColumn, TableView, and QAbstractTableModel.

Property Documentation

[read-only] columnCount : int

This read-only property holds the number of columns in the model.

The number of columns is fixed for the lifetime of the model after the rows property is set or appendRow() is called for the first time.


[read-only] rowCount : int

This read-only property holds the number of rows in the model.

This value changes whenever rows are added or removed from the model.


rows : object

This property holds the model data in the form of an array of rows:

            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
                }
            ]

See also getRow(), setRow(), moveRow(), appendRow(), insertRow(), clear(), rowCount, and columnCount.


Method Documentation

appendRow(object row)

Adds a new row to the end of the model, with the values (cells) in row.

model.appendRow({
    checkable: true,
    amount: 1,
    fruitType: "Pear",
    fruitName: "Williams",
    fruitPrice: 1.50,
})

See also insertRow(), setRow(), and removeRow().


clear()

Removes all rows from the model.

See also removeRow().


variant data(QModelIndex index, string role)

Returns the data from the table cell at the given index belonging to the given role.

See also setData() and index().


object getRow(int rowIndex)

Returns the row at rowIndex in the model.

Note that this equivalent to accessing the row directly through the rows property:

Component.onCompleted: {
    // These two lines are equivalent.
    console.log(model.getRow(0).display);
    console.log(model.rows[0].fruitName);
}

Note: the returned object cannot be used to modify the contents of the model; use setRow() instead.

See also setRow(), appendRow(), insertRow(), removeRow(), and moveRow().


QModelIndex index(int row, int column)

Returns a QModelIndex object referencing the given row and column, which can be passed to the data() function to get the data from that cell, or to setData() to edit the contents of that cell.

import QtQml 2.14
import Qt.labs.qmlmodels 1.0

TableModel {
    id: model

    TableModelColumn { display: "fruitType" }
    TableModelColumn { display: "fruitPrice" }

    rows: [
        { fruitType: "Apple", fruitPrice: 1.50 },
        { fruitType: "Orange", fruitPrice: 2.50 }
    ]

    Component.onCompleted: {
        for (var r = 0; r < model.rowCount; ++r) {
            console.log("An " + model.data(model.index(r, 0)).display +
                        " costs " + model.data(model.index(r, 1)).display.toFixed(2))
        }
    }
}

See also QModelIndex and related Classes in QML and data().


insertRow(int rowIndex, object row)

Adds a new row to the list model at position rowIndex, with the values (cells) in row.

model.insertRow(2, {
    checkable: true, checked: false,
    amount: 1,
    fruitType: "Pear",
    fruitName: "Williams",
    fruitPrice: 1.50,
})

The rowIndex must be to an existing item in the list, or one past the end of the list (equivalent to appendRow()).

See also appendRow(), setRow(), removeRow(), and rowCount.


moveRow(int fromRowIndex, int toRowIndex, int rows)

Moves rows from the index at fromRowIndex to the index at toRowIndex.

The from and to ranges must exist; for example, to move the first 3 items to the end of the list:

model.moveRow(0, model.rowCount - 3, 3)

See also appendRow(), insertRow(), removeRow(), and rowCount.


removeRow(int rowIndex, int rows = 1)

Removes the row at rowIndex from the model.

See also clear() and rowCount.


bool setData(QModelIndex index, string role, variant value)

Inserts or updates the data field named by role in the table cell at the given index with value. Returns true if sucessful, false if not.

See also data() and index().


setRow(int rowIndex, object row)

Changes the row at rowIndex in the model with row.

All columns/cells must be present in row, and in the correct order.

model.setRow(0, {
    checkable: true,
    amount: 1,
    fruitType: "Pear",
    fruitName: "Williams",
    fruitPrice: 1.50,
})

If rowIndex is equal to rowCount(), then a new row is appended to the model. Otherwise, rowIndex must point to an existing row in the model.

See also appendRow(), insertRow(), and rowCount.


© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.