本页内容

TableViewDelegate QML Type

可分配给TableView 的委托对象。更多内容...

Import Statement: import QtQuick.Controls
Since: Qt 6.9
Inherits:

ItemDelegate

Inherited By:

HorizontalHeaderViewDelegate and VerticalHeaderViewDelegate

属性

详细说明

使用表格视图委托显示地址的表格视图

TableViewDelegate 是一种可分配给TableView 的delegate property 的委托。它会根据应用程序的样式,在视图中渲染表格的每个单元格。

TableView {
    anchors.fill: parent
    delegate: TableViewDelegate {}
    // model: yourModel
}

TableViewDelegate 继承自ItemDelegate ,这意味着它由两个组成部分构成:background 和contentItem 。

contentItem的位置由padding 控制。

与指针的交互

TableViewDelegate 继承自ItemDelegate 。这意味着当用户点击委托时,它会发出诸如clicked 之类的信号。您可以连接到该信号,以实现特定于应用程序的功能。

不过,ItemDelegate 的 API 不会提供点击位置或按下的修饰键等信息。如果需要这些信息,更好的方法是使用指针处理程序,例如:

TableView {
    id: tableView
    delegate: TableViewDelegate {
        TapHandler {
            acceptedButtons: Qt.RightButton
            onTapped: someContextMenu.open()
        }

        TapHandler {
            acceptedModifiers: Qt.ControlModifier
            onTapped: tableView.doSomethingToCell(row, column)
        }
    }
}

注意:若要 禁用用户点击委托时触发的默认行为(如更改当前索引),可将 `pointerNavigationEnabled ` 设置为 `false`。

编辑表格中的单元格

TableViewDelegate 已分配了一个默认的edit delegate 。如果TableView 的edit triggers 属性已设置,且model 支持editing model items ,则用户可以触发任何编辑触发器来编辑current 表格单元格中的文本。

默认编辑委托将使用Qt.EditRole 从model 读取和写入数据。如果您需要使用其他角色,或者有超出默认编辑委托功能范围的其他需求,可以随时将您自己的委托分配给TableView.editDelegate 。

另请参阅 《自定义 TableViewDelegate和TableView 》。

属性文档

current : bool

无论该委托人是否代表selection model 中的current index ,此属性均成立。

editing : bool

无论委托是否正在被edited ,该属性均保持有效。

selected : bool

无论该委托人是否代表selection model 中的selected index ,此属性均成立。

tableView : TableView

该属性指向包含委托项的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.