本页内容

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
Qt::DisplayRoledisplay
Qt::DecorationRoledecoration
Qt::EditRoleedit
Qt::ToolTipRole工具提示
Qt::StatusTipRolestatusTip
Qt::WhatsThisRole这是什么
Qt::FontRole字体
Qt::TextAlignmentRole文本对齐
Qt::BackgroundRolebackground
Qt::ForegroundRole前景色
Qt::CheckStateRole复选框状态
Qt::AccessibleTextRole可访问文本
Qt::AccessibleDescriptionRole辅助描述
Qt::SizeHintRole大小提示角色名称

另请参阅 TableModel 和TableView 。

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