TreeView QML类型

TreeView QML Type

TreeView QML类型

Provides a tree view to display data from a QAbstractItemModelMore...

​提供树视图以显示QAbstractItemModel中的数据。更多。。。

Import Statement:

导入语句:

import QtQuick

Since:

自:

Qt 6.3

Inherits:

继承:

TableView

Signals

信号

Methods

方法

Detailed Description

详细描述

A TreeView has a model that defines the data to be displayed, and a delegate that defines how the data should be displayed.

​TreeView有一个定义要显示的数据的模型,以及一个定义如何显示数据的委托。

TreeView inherits TableView. This means that even if the model has a parent-child tree structure, TreeView is internally using a proxy model that converts that structure into a flat table model that can be rendered by TableView. Each node in the tree ends up occupying one row in the table, where the first column renders the tree itself. By indenting each delegate item in that column according to its parent-child depth in the model, it will end up looking like a tree, even if it's technically still just a flat list of items.

​TreeView继承TableView。这意味着,即使模型具有父子树结构,TreeView也在内部使用代理模型,该代理模型将该结构转换为TableView可以呈现的平面表模型。树中的每个节点最终占据表中的一行,其中第一列呈现树本身。通过根据模型中的父子深度缩进该列中的每个委托项,它最终将看起来像一棵树,即使从技术上讲它仍然只是一个简单的项列表。

To allow for maximum flexibility, TreeView itself will not position the delegate items into a tree structure. This burden is placed on the delegate. Qt Quick Controls offers a ready-made TreeViewDelegate that can be used for this, which has the advantage that it works out-of-the-box and renders a tree which follows the style of the platform where the application runs.

​为了实现最大的灵活性,TreeView本身不会将委托项定位到树结构中。这一负担由委托承担。Qt Quick Controls提供了一个现成的TreeViewDelegate,可以用于此目的,其优点是它可以开箱即用,并呈现一个遵循应用程序运行的平台样式的树。

Even if TreeViewDelegate is customizable, there might be situations where you want to render the tree in a different way, or ensure that the delegate ends up as minimal as possible, perhaps for performance reasons. Creating your own delegate from scratch is easy, since TreeView offers a set of properties that can be used to position and render each node in the tree correctly.

​即使TreeViewDelegate是可自定义的,也可能存在这样的情况,即您希望以不同的方式呈现树,或确保委托的结束尽可能少,这可能是出于性能原因。从头开始创建自己的委托很容易,因为TreeView提供了一组属性,可用于正确定位和渲染树中的每个节点。

An example of a custom delegate is shown below:

自定义委托的示例如下所示:

import QtQuick

Window {
    width: 600
    height: 400
    visible: true

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

        delegate: Item {
            id: treeDelegate

            implicitWidth: padding + label.x + label.implicitWidth + padding
            implicitHeight: label.implicitHeight * 1.5

            readonly property real indent: 20
            readonly property real padding: 5

            // Assigned to by TreeView:
            required property TreeView treeView
            required property bool isTreeNode
            required property bool expanded
            required property int hasChildren
            required property int depth

            TapHandler {
                onTapped: treeView.toggleExpanded(row)
            }

            Text {
                id: indicator
                visible: treeDelegate.isTreeNode && treeDelegate.hasChildren
                x: padding + (treeDelegate.depth * treeDelegate.indent)
                anchors.verticalCenter: label.verticalCenter
                text: "▸"
                rotation: treeDelegate.expanded ? 90 : 0
            }

            Text {
                id: label
                x: padding + (treeDelegate.isTreeNode ? (treeDelegate.depth + 1) * treeDelegate.indent : 0)
                width: treeDelegate.width - treeDelegate.padding - x
                clip: true
                text: model.display
            }
        }
    }
}

The properties that are marked as required will be filled in by TreeView, and are similar to attached properties. By marking them as required, the delegate indirectly informs TreeView that it should take responsibility for assigning them values. The following required properties can be added to a delegate:

标记为必需的属性将由TreeView填充,与附加属性类似。通过将它们标记为所需,委托间接通知TreeView它应该负责为它们分配值。可以将以下必需属性添加到委托中:

  • required property TreeView treeView - Points to the TreeView that contains the delegate item.
  • required property TreeView treeView - 指向包含委托项的TreeView。
  • required property bool isTreeNode - Is true if the delegate item represents 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 should typically be 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, and will show data from the remaining columns in the model (and typically not be indented).
  • required property bool isTreeNode -如果委托项表示树中的节点,则为true。视图中只有一列将用于绘制树,因此,只有该列中的委托项将此属性设置为true。树中的节点通常应该根据深度缩进,如果hasChildren为true,则显示一个指示符。其他列中的委托项将此属性设置为false,并显示模型中其余列的数据(通常不会缩进)。
  • required property bool expanded - Is true if the model item drawn by the delegate is expanded in the view.
  • required property bool expanded -如果委托绘制的模型项在视图中展开,则为true。
  • required property int hasChildren - Is true if the model item drawn by the delegate has children in the model.
  • required property int hasChildren -如果委托绘制的模型项在模型中有子项,则为true。
  • required property int depth - Contains 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.
  • required property int depth -包含委托绘制的模型项的深度。模型项的深度与它在模型中的祖先数量相同。

See also Required Properties.

​另请参见必需属性。

Note: A TreeView only accepts a model that inherits QAbstractItemModel.

​注意:TreeView只接受继承QAbstractItemModel的模型。

Signal Documentation

信号文档

collapsed(rowrecursively)

This signal is emitted when a row is collapsed in the view. row will be equal to the argument given to the call that caused the collapse to happen (collapse() or collapseRecursively()). If the row was collapsed recursively, recursively will be true.

​当视图中的行折叠时,会发出此信号。行将等于为导致崩溃发生的调用(collapse()或collapseRecursively())提供的参数。如果该行是递归折叠的,则递归为true。

Note: when a row is collapsed recursively, the collapsed signal will only be emitted for that one row, and not for its descendants.

注意:当一行以递归方式折叠时,折叠的信号将仅为该行发出,而不会为其后代发出。

Note: The corresponding handler is onCollapsed.

注意:相应的处理器为onCollapsed。

See also expanded(), expand(), collapse(), and toggleExpanded().

​请参见expanded()、expand()、collapse()和toggleExpended()。

expanded(rowdepth)

This signal is emitted when a row is expanded in the view. row and depth will be equal to the arguments given to the call that caused the expansion to happen (expand() or expandRecursively()). In case of expand(), depth will always be 1. In case of expandToIndex(), depth will be the depth of the target index.

​当视图中的行展开时,会发出此信号。行和深度将等于为导致扩展发生的调用(expand()或expandRecursively())提供的参数。如果是expand(),深度将始终为1。如果是expandToIndex(),则深度将是目标索引的深度。

Note: when a row is expanded recursively, the expanded signal will only be emitted for that one row, and not for its descendants.

注意:当一行以递归方式展开时,将仅为该行发出展开信号,而不会为其后代发出展开信号。

Note: The corresponding handler is onExpanded.

注意:相应的处理器是onExpanded。

See also collapsed(), expand(), collapse(), and toggleExpanded().

​请参见collapsed()、expand()、collapse()和toggleExpanded()。

Method Documentation

方法文档

collapse(row)

Collapses the tree node at the given row in the view.

折叠视图中给定行处的树节点。

row should be the row in the view (table row), and not a row in the model.

行应该是视图中的行(表行),而不是模型中的行。

Note: this function will not affect the model, only the visual representation in the view.

注意:此功能不会影响模型,只会影响视图中的视觉显示。

See also expand() and isExpanded().

​请参见expand()和isExpanded()。

[since 6.4]collapseRecursively(row = -1)

Collapses the tree node at the given row in the view recursively down to all leaves.

将视图中给定行处的树节点递归向下折叠到所有叶。

For a model has more than one root, you can also call this function with row equal to -1. This will collapse all roots. Hence, calling collapseRecursively(-1), or simply collapseRecursively(), will collapse all nodes in the model.

对于一个模型有多个根,您也可以使用行等于-1的行调用此函数。这将折叠所有根。因此,调用collapseSecurityly(-1)或简单地调用collapseSecurityly()将折叠模型中的所有节点。

row should be the row in the view (table row), and not a row in the model.

行应该是视图中的行(表行),而不是模型中的行。

Note: this function will not affect the model, only the visual representation in the view.

注意:此功能不会影响模型,只会影响视图中的视觉显示。

This method was introduced in Qt 6.4.

Qt 6.4中介绍了该方法。

See also expandRecursively(), expand(), collapse(), isExpanded(), and depth().

​另请参见expandRecursively()、expand()、collapse()、isExpanded()和depth()。

int depth(row)

Returns the depth (the number of parents up to the root) of the given row.

返回给定行的深度(达到根的父级数)。

row should be the row in the view (table row), and not a row in the model. If row is not between 0 and rows, the return value will be -1.

​行应该是视图中的行(表行),而不是模型中的行。如果行不在0和rows之间,则返回值为-1。

See also modelIndex().

另请参见modelIndex()。

expand(row)

Expands the tree node at the given row in the view.

展开视图中给定行处的树节点。

row should be the row in the view (table row), and not a row in the model.

行应该是视图中的行(表行),而不是模型中的行。

Note: this function will not affect the model, only the visual representation in the view.

注意:此功能不会影响模型,只会影响视图中的视觉显示。

See also collapse(), isExpanded(), and expandRecursively().

请参见collapse()、isExpanded()和expandRecursively()。

[since 6.4]expandRecursively(row = -1, depth = -1)

Expands the tree node at the given row in the view recursively down to depthdepth should be relative to the depth of row. If depth is -1, the tree will be expanded all the way down to all leaves.

将视图中给定行处的树节点递归向下展开到深度。深度应该相对于行的深度。如果深度为-1,树将一直向下展开到所有叶子。

For a model that has more than one root, you can also call this function with row equal to -1. This will expand all roots. Hence, calling expandRecursively(-1, -1), or simply expandRecursively(), will expand all nodes in the model.

对于具有多个根的模型,也可以使用行等于-1的行调用此函数。这将扩展所有根。因此,调用expandRecurrively(-1,-1),或者简单地调用expand递归,将扩展模型中的所有节点。

row should be the row in the view (table row), and not a row in the model.

行应该是视图中的行(表行),而不是模型中的行。

Note: This function will not try to fetch more data.

​注意:此函数不会尝试获取更多数据。

Note: This function will not affect the model, only the visual representation in the view.

注意:此功能不会影响模型,只会影响视图中的视觉显示。

Warning: If the model contains a large number of items, this function will take some time to execute.

警告:如果模型包含大量项,则执行此函数需要一些时间。

This method was introduced in Qt 6.4.

Qt 6.4中介绍了该方法。

See also collapseRecursively(), expand(), collapse(), isExpanded(), and depth().

​请参见collapseRecursively()、expand()、collapse()、isExpanded()和depth()。

[since 6.4]expandToIndex(QModelIndex index)

Expands the tree from the given model index, and recursively all the way up to the root. The result will be that the delegate item that represents index becomes visible in the view (unless it ends up outside the viewport). To ensure that the row ends up visible in the viewport, you can do:

从给定的模型索引展开树,并一直递归到根。结果将是表示索引的委托项在视图中可见(除非它在视口外结束)。要确保行在视口中可见,可以执行以下操作:

​expandToIndex(index) 
forceLayout() 
positionViewAtRow(rowAtIndex(index), Qt.AlignVCenter)

This method was introduced in Qt 6.4.

Qt 6.4中介绍了该方法。

See also expand() and expandRecursively().

​另请参见expand()和expandRecursively()。

bool isExpanded(row)

Returns if the given row in the view is shown as expanded.

返回视图中的给定行是否显示为展开。

row should be the row in the view (table row), and not a row in the model. If row is not between 0 and rows, the return value will be false.

​行应该是视图中的行(表行),而不是模型中的行。如果行不在0和rows之间,则返回值为false。

toggleExpanded(row)

Toggles if the tree node at the given row should be expanded. This is a convenience for doing:

切换是否应展开给定行的树节点。这样做很方便:

if (isExpanded(row))
    collapse(row)
else
    expand(row)

row should be the row in the view (table row), and not a row in the model.

行应该是视图中的行(表行),而不是模型中的行。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值