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 。
支持的角色
TableModelColumn 支持Qt's roles 中的所有角色,但Qt::InitialSortOrderRole 除外。可通过以下方式访问角色,例如:
text: display
required property string display| Qt::DisplayRole | display |
| Qt::DecorationRole | decoration |
| Qt::EditRole | edit |
| Qt::ToolTipRole | 工具提示 |
| Qt::StatusTipRole | statusTip |
| Qt::WhatsThisRole | 这是什么 |
| Qt::FontRole | 字体 |
| Qt::TextAlignmentRole | 文本对齐 |
| Qt::BackgroundRole | background |
| 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.