HorizontalHeaderView QML Type
Import Statement: | import QtQuick.Controls |
Inherits: |
属性
- model : QVariant
- movableColumns : bool
(since 6.8)
- syncView : TableView
- textRole : string
详细说明
HorizontalHeaderView 提供了一个风格化的表头。它既可用作独立视图,也可用作TableView 的表头。
您可以将TableView 指定为 HorizontalHeaderView 的syncView 属性,从而为TableView 添加表头。这样,页眉和表格就会在翻页时保持同步。
默认情况下,HorizontalHeaderView 会显示来自sync view's model 的header data 。如果您不想使用该模型中的页眉数据,或者您不使用syncView ,您可以显式地为model 属性指定一个模型。
注意: 为了显示QAbstractItemModel 的页眉数据,HorizontalHeaderView 将在内部用一个独立的代理模型来封装模型的页眉数据。该模型不与application model 共享任何模型项。这意味着,如果调用index() 等函数,返回的模型索引将属于代理模型,而不是应用程序模型。
默认情况下,textRole 设置为"display"
,这意味着将使用模型Qt::DisplayRole 中的数据。您可以将其设置为另一个角色名称,以便显示该数据。
应用程序负责将标题放置在场景中的正确位置。您可以在一个TableView 上添加任意数量的页眉,如果您想在表格的四边都放置页眉,这将非常有用。
以下代码段展示了如何在表格视图中添加水平和垂直标题视图:
import QtQuick import QtQuick.Controls import Qt.labs.qmlmodels ApplicationWindow { visible: true width: 640 height: 480 title: qsTr("HeaderView") Rectangle { anchors.fill: parent // The background color will show through the cell // spacing, and therefore become the grid line color. color: Application.styleHints.appearance === Qt.Light ? palette.mid : palette.midlight HorizontalHeaderView { id: horizontalHeader anchors.left: tableView.left anchors.top: parent.top syncView: tableView clip: true } VerticalHeaderView { id: verticalHeader anchors.top: tableView.top anchors.left: parent.left syncView: tableView clip: true } TableView { id: tableView anchors.left: verticalHeader.right anchors.top: horizontalHeader.bottom anchors.right: parent.right anchors.bottom: parent.bottom clip: true columnSpacing: 1 rowSpacing: 1 model: TableModel { TableModelColumn { display: "name" } TableModelColumn { display: "color" } rows: [ { "name": "cat", "color": "black" }, { "name": "dog", "color": "brown" }, { "name": "bird", "color": "white" } ] } delegate: Rectangle { implicitWidth: 100 implicitHeight: 20 color: palette.base Label { text: display } } } } }
HorizontalHeaderView 的resizableColumns 默认设置为true
。
另请参阅 VerticalHeaderView 。
属性文档
model : QVariant |
该属性包含为水平标题视图提供数据的模型。
默认情况下,水平标题视图显示来自sync view's model 的header data 。如果不想使用来自该模型的页眉数据,或者不使用syncView ,则可以为该属性明确指定一个模型。如果model 是QAbstractTableModel ,则将使用它的header data 。否则,如果它是QAbstractItemModel ,将使用data 。
除了QAbstractItemModels 之外,还可以为该属性指定其他类型的模型,例如 JavaScript 数组。
另请参阅 TableView 、model 和QAbstractTableModel 。
movableColumns : bool |
此属性允许用户在视图中移动列。默认值为false
。
注: 如果设置了此属性,HorizontalHeaderView ,用户就可以在所需位置拖放列。启用syncView 后,任何更改都将应用到相应的视图项。
此属性在 Qt 6.8 中引入。
syncView : TableView |
textRole : string |
该属性保存用于在每个页眉单元格中显示文本的模型角色。
当模型有多个角色时,可通过设置 textRole 来确定应显示哪个角色。
如果模型是QAbstractItemModel ,则默认为 "显示";否则为空。
如果模型的roleNames() 没有提供 textRole 中指定的角色,则会发出警告。可以通过设置 textRole 来消除警告。
另请参阅 QAbstractItemModel::roleNames()。
© 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.