TreeViewDelegate QML Type

可分配给TreeView 的委托...更多...

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

ItemDelegate

属性

详细说明

TreeViewDelegate 是一个可分配给delegate propertyTreeView 的委托。它使用应用程序样式在视图中渲染树以及其他列。

TreeView {
    anchors.fill: parent
    delegate: TreeViewDelegate {}
    // The model needs to be a QAbstractItemModel
    // model: yourTreeModel
}

TreeViewDelegate 继承于ItemDelegate ,这意味着它由三个项组成:一个background 、一个contentItem 和一个indicator 。TreeViewDelegate 会根据内容项和指示器在树中的位置来处理indenting 。只有当委托项位于tree column 内时,指示器才会可见,并渲染模型项with children

如果您更改了指示符,它将不再默认缩进。相反,您需要考虑depthindentation ,通过设置指标的x position 来自行缩进。下面是一个如何缩进的示例:

TreeViewDelegate {
    indicator: Item {
        x: leftMargin + (depth * indentation)
    }
}

contentItem 的位置由padding 控制。这意味着您可以更改 contentItem,而无需处理缩进问题。但这也意味着,应避免将 padding 改为其他内容,因为这样会移除缩进。指示符左侧的空间由leftMargin 控制。指示符与 contentItem 之间的空格由spacing 控制。内容项右侧的空格由rightMargin 控制。

与指针交互

TreeViewDelegate 继承了ItemDelegate 。这意味着当用户点击委托时,它将发出clicked 等信号。如果需要,除了默认的展开/折叠行为外,您还可以连接到该信号来实现应用程序的特定功能(甚至可以将pointerNavigationEnabled 设置为false ,以禁用默认行为)。

但是,ItemDelegate API 并不提供点击位置的信息,也不提供哪些修改器被保留的信息。如果需要这些信息,更好的办法是使用指针处理程序等:

TreeView {
    id: treeView
    delegate: TreeViewDelegate {
        TapHandler {
            acceptedButtons: Qt.RightButton
            onTapped: someContextMenu.open()
        }

        TapHandler {
            acceptedModifiers: Qt.ControlModifier
            onTapped: {
                if (treeView.isExpanded(row))
                    treeView.collapseRecursively(row)
                else
                    treeView.expandRecursively(row)
            }
        }
    }
}

注: 如果想禁用用户点击委托时发生的默认行为(如更改当前索引),可将pointerNavigationEnabled 设置为false

编辑树中的节点

TreeViewDelegate 已分配了一个默认的edit delegate 。如果TreeView 设置了edit triggers ,而model 又支持editing model items ,那么用户就可以激活任何一个编辑触发器来编辑current 树节点的文本。

默认的编辑委托将尝试使用Qt.EditRole 来读取和写入model 的数据。如果QAbstractItemModel::data() 返回该角色的空字符串,则将使用Qt.DisplayRole 代替。

如果您的需求超出了默认编辑委托所提供的范围,您可以随时将自己的自定义编辑委托分配给TableView.editDelegate

另请参阅 TreeView

属性文档

current : bool

此属性表示委托是否代表current index 中的selection model


depth : int

该属性表示委托绘制的模型项的深度。模型项的深度与它在模型中的祖先数相同。


editing : bool [since 6.5]

如果委托正在绘制模型项,则该属性保持不变。edited.

此属性在 Qt 6.5 中引入。


expanded : bool

如果委托绘制的模型项在视图中展开,则该属性为true


hasChildren : bool

如果委托绘制的模型项在模型中有子代,则此属性为true


indentation : real

该属性表示子代相对于父代水平缩进的空间。


isTreeNode : bool

如果委托项绘制的是树中的节点,则此属性为true 。视图中只有一列用于绘制树,因此只有该列中的委托项才会将此属性设置为true

树中的节点根据其depth 显示为indented ,如果hasChildren 显示为true ,则显示indicator 。其他列中的委托项将被设置为false


leftMargin : real

该属性保留视图左边缘与指示符左边缘之间的空间(除缩进外)。如果不可见指示符,空间将位于视图左边缘和内容项左边缘之间。

另请参阅 rightMargin,indentation, 和spacing


rightMargin : real

该属性保留视图右边缘和内容项右边缘之间的空间。

另请参阅 leftMargin,indentation, 和spacing


selected : bool

如果委托代表selection model 中的selected index ,则该属性成立。


treeView : TreeView

该属性指向包含委托项的TreeView


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