TableModelColumn QML Type

代表模型中的一列。更多

Import Statement: import Qt.labs.qmlmodels

详细描述

TableModelColumn 类代表TableModel 中的列。TableModel 支持 JavaScript/JSON 数据,其中每一行都是一个对象,是一个简单的键值对列表,其中的键是无序的。

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

然而,Qt 中的模型是通过行和列索引来操作的。使用 TableModelColumn 指定列可以将 Qt 的内置角色映射到每一行对象中的任何属性。

import QtQuick
import QtQuick.Window
import Qt.labs.qmlmodels

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

TableModelColumn 还为复杂行提供基本的只读支持。更多信息,请参阅Supported Row Data Structures

注: 上述大部分概念也适用于TreeModel ,只是在TreeModel 中,每一行代表树的一个节点。

支持的角色

TableModelColumn 支持Qt's roles 中的所有角色,但Qt::InitialSortOrderRole 除外。角色可通过以下方式访问,例如

text: display

required property string display

另请参阅 TableModelTableView

© 2025 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.