![]() |
Qt 6.x
The Qt SDK
|
#include "qquicktreeview_p_p.h"
#include <QtCore/qobject.h>
#include <QtQml/qqmlcontext.h>
#include <QtQuick/private/qquicktaphandler_p.h>
#include <QtQmlModels/private/qqmltreemodeltotablemodel_p_p.h>
#include "moc_qquicktreeview_p.cpp"
Go to the source code of this file.
Variables | |
static const int | kTreeColumn = 0 |
\qmltype TreeView \inqmlmodule QtQuick | |
|
static |
\qmltype TreeView \inqmlmodule QtQuick
Provides a tree view to display data from a QAbstractItemModel.
A TreeView has a \l model that defines the data to be displayed, and a \l delegate that defines how the data should be displayed.
TreeView inherits \l 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.
To allow for maximum flexibility, TreeView itself will not position the delegate items into a tree structure. This burden is placed on the delegate. \l {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.
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.
An example of a custom delegate is shown below:
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:
\list
{required
property TreeView treeView}{required
property bool isTreeNode}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 expanded}true
if the model item drawn by the delegate is expanded in the view. {required
property bool hasChildren}true
if the model item drawn by the delegate has children in the model. {required
property int depth}See also \l {Required Properties}.
By default, TreeView \l {toggleExpanded()}{toggles} the expanded state of a row when you double tap on it. Since this is in conflict with double tapping to edit a cell, TreeView sets \l {TableView::}{editTriggers} to TableView.EditKeyPressed
by default (which is different from TableView, which uses {TableView.EditKeyPressed
| TableView.DoubleTapped}. If you change \l {TableView::}{editTriggers} to also contain TableView.DoubleTapped
, toggling the expanded state with a double tap will be disabled.
\qmlproperty QModelIndex QtQuick::TreeView::rootIndex
This property holds the model index of the root item in the tree. By default, this is the same as the root index in the model, but you can set it to be a child index instead, to show only a branch of the tree. Set it to undefined
to show the whole model.
\qmlmethod int QtQuick::TreeView::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 \l {TableView::}{rows}, the return value will be -1
.
\qmlmethod bool QtQuick::TreeView::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 \l {TableView::}{rows}, the return value will be false
.
\qmlmethod QtQuick::TreeView::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.
\qmlmethod QtQuick::TreeView::expandRecursively(row = -1, depth = -1)
Expands the tree node at the given row in the view recursively down to depth. depth should be relative to the depth of row. If depth is -1
, the tree will be expanded all the way down to all leaves.
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.
row should be the row in the view (table row), and not a row in the model.
\qmlmethod QtQuick::TreeView::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:
\qmlmethod QtQuick::TreeView::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.
\qmlmethod QtQuick::TreeView::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.
row should be the row in the view (table row), and not a row in the model.
\qmlmethod QtQuick::TreeView::toggleExpanded(row)
Toggles if the tree node at the given row should be expanded. This is a convenience for doing:
row should be the row in the view (table row), and not a row in the model.
\qmlsignal QtQuick::TreeView::expanded(row, depth)
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 (\l expand() or \l expandRecursively()). In case of \l expand(), depth will always be 1
. In case of \l expandToIndex(), depth will be the depth of the target index.
\qmlsignal QtQuick::TreeView::collapsed(row, recursively)
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 (\l collapse() or \l collapseRecursively()). If the row was collapsed recursively, recursively will be true
.
Definition at line 259 of file qquicktreeview.cpp.
Referenced by QQuickTreeViewPrivate::updateRequiredProperties().