TreeViewDelegate QML Type

A delegate that can be assigned to a TreeView . 更多...

import 语句: import QtQuick.Controls
Since: Qt 6.3
继承:

ItemDelegate

特性

详细描述

A TreeViewDelegate is a delegate that can be assigned to the delegate property TreeView . It renders the tree, as well as the other columns, in the view using the application style.

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

TreeViewDelegate inherits ItemDelegate , which means that it's composed of three items: a background contentItem , and an indicator . TreeViewDelegate takes care of indenting the contentItem and the indicator according their location in the tree. The indicator will only be visible if the delegate item is inside the tree column , and renders a model item with children .

If you change the indicator, it will no longer be indented by default. Instead you need to indent it yourself by setting the x position of the indicator, taking the depth and indentation into account. Below is an example of how to do that:

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

The position of the contentItem is controlled with padding . This means that you can change the contentItem without dealing with indentation. But it also means that you should avoid changing padding to something else, as that will remove the indentation. The space to the left of the indicator is instead controlled with leftMargin . The space between the indicator and the contentItem is controlled with spacing . And the space to the right of the contentItem is controlled with rightMargin .

Interacting with pointers

TreeViewDelegate inherits ItemDelegate . This means that it will emit signals such as clicked when the user clicks on the delegate. If needed, you could connect to that signal to implement application specific functionality, in addition to the default expand/collapse behavior (and even set pointerNavigationEnabled to false , to disable the default behavior as well).

But the ItemDelegate API does not give you information about the position of the click, or which modifiers are being held. If this is needed, a better approach would be to use pointer handlers, for example:

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)
            }
        }
    }
}
					

注意: If you want to disable the default behavior that occurs when the user clicks on the delegate (like changing the current index), you can set pointerNavigationEnabled to false .

Editing nodes in the tree

TreeViewDelegate has a default edit delegate assigned. If TreeView has edit triggers set, and the model has support for editing model items , then the user can activate any of the edit triggers to edit the text of the current tree node.

The default edit delegate will try to use the Qt.EditRole to read and write data to the model 。若 QAbstractItemModel::data () returns an empty string for this role, then Qt.DisplayRole will be used instead.

You can always assign your own custom edit delegate to TableView.editDelegate if you have needs outside what the default edit delegate offers.

另请参阅 TreeView .

特性文档编制

current : bool

This property holds if the delegate represent the current index selection model .

depth : int

This property holds the depth of the model item drawn by the delegate. The depth of a model item is the same as the number of ancestors it has in the model.

editing : bool [since 6.5]

This property holds if the delegate is being edited.

该特性在 Qt 6.5 引入。

expanded : bool

此特性是 true if the model item drawn by the delegate is expanded in the view.

hasChildren : bool

此特性是 true if the model item drawn by the delegate has children in the model.

indentation : real

This property holds the space a child is indented horizontally relative to its parent.

isTreeNode : bool

此特性是 true if the delegate item draws a node in the tree. Only one column in the view will be used to draw the tree, and therefore, only delegate items in that column will have this property set to true .

A node in the tree is indented according to its depth , and show an indicator if hasChildren is true . Delegate items in other columns will have this property set to false .

leftMargin : real

This property holds the space between the left edge of the view and the left edge of the indicator (in addition to indentation). If no indicator is visible, the space will be between the left edge of the view and the left edge of the contentItem.

另请参阅 rightMargin , indentation ,和 spacing .

rightMargin : real

This property holds the space between the right edge of the view and the right edge of the contentItem.

另请参阅 leftMargin , indentation ,和 spacing .

selected : bool

This property holds if the delegate represent a selected index selection model .

treeView : TreeView

This property points to the TreeView that contains the delegate item.