QTreeView Class
The QTreeView class provides a default model/view implementation of a tree view. More...
| Header: | #include <QTreeView> |
| qmake: | QT += widgets |
| Inherits: | QAbstractItemView |
| Inherited By: |
Properties
|
|
Public Functions
| bool | allColumnsShowFocus() const |
| int | autoExpandDelay() const |
| bool | expandsOnDoubleClick() const |
| int | indentation() const |
| bool | isAnimated() const |
| bool | isHeaderHidden() const |
| bool | isSortingEnabled() const |
| bool | itemsExpandable() const |
| void | resetIndentation() |
| bool | rootIsDecorated() const |
| void | setAllColumnsShowFocus(bool enable) |
| void | setAnimated(bool enable) |
| void | setAutoExpandDelay(int delay) |
| void | setExpandsOnDoubleClick(bool enable) |
| void | setHeaderHidden(bool hide) |
| void | setIndentation(int i) |
| void | setItemsExpandable(bool enable) |
| void | setRootIsDecorated(bool show) |
| void | setSortingEnabled(bool enable) |
| void | setUniformRowHeights(bool uniform) |
| void | setWordWrap(bool on) |
| bool | uniformRowHeights() const |
| bool | wordWrap() const |
Detailed Description

A QTreeView implements a tree representation of items from a model. This class is used to provide standard hierarchical lists that were previously provided by the QListView class, but using the more flexible approach provided by Qt's model/view architecture.
The QTreeView class is one of the Model/View Classes and is part of Qt's model/view framework.
QTreeView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class.
It is simple to construct a tree view displaying data from a model. In the following example, the contents of a directory are supplied by a QFileSystemModel and displayed as a tree:
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);
The model/view architecture ensures that the contents of the tree view are updated as the model changes.
Items that have children can be in an expanded (children are visible) or collapsed (children are hidden) state. When this state changes a collapsed() or expanded() signal is emitted with the model index of the relevant item.
The amount of indentation used to indicate levels of hierarchy is controlled by the indentation property.
Headers in tree views are constructed using the QHeaderView class and can be hidden using header()->hide(). Note that each header is configured with its stretchLastSection property set to true, ensuring that the view does not waste any of the space assigned to it for its header. If this value is set to true, this property will override the resize mode set on the last section in the header.
By default, all columns in a tree view are movable except the first. To disable movement of these columns, use QHeaderView's setSectionsMovable() function. For more information about rearranging sections, see Moving Header Sections.
Key Bindings
QTreeView supports a set of key bindings that enable the user to navigate in the view and interact with the contents of items:
| Key | Action |
|---|---|
| Up | Moves the cursor to the item in the same column on the previous row. If the parent of the current item has no more rows to navigate to, the cursor moves to the relevant item in the last row of the sibling that precedes the parent. |
| Down | Moves the cursor to the item in the same column on the next row. If the parent of the current item has no more rows to navigate to, the cursor moves to the relevant item in the first row of the sibling that follows the parent. |
| Left | Hides the children of the current item (if present) by collapsing a branch. |
| Minus | Same as Left. |
| Right | Reveals the children of the current item (if present) by expanding a branch. |
| Plus | Same as Right. |
| Asterisk | Expands the current item and all its children (if present). |
| PageUp | Moves the cursor up one page. |
| PageDown | Moves the cursor down one page. |
| Home | Moves the cursor to an item in the same column of the first row of the first top-level item in the model. |
| End | Moves the cursor to an item in the same column of the last row of the last top-level item in the model. |
| F2 | In editable models, this opens the current item for editing. The Escape key can be used to cancel the editing process and revert any changes to the data displayed. |
Improving Performance
It is possible to give the view hints about the data it is handling in order to improve its performance when displaying large numbers of items. One approach that can be taken for views that are intended to display items with equal heights is to set the uniformRowHeights property to true.
See also QListView, QTreeWidget, View Classes, QAbstractItemModel, QAbstractItemView, and Dir View Example.
Property Documentation
allColumnsShowFocus : bool
This property holds whether items should show keyboard focus using all columns
If this property is true all columns will show focus, otherwise only one column will show focus.
The default is false.
This property was introduced in Qt 4.2.
Access functions:
| bool | allColumnsShowFocus() const |
| void | setAllColumnsShowFocus(bool enable) |
animated : bool
This property holds whether animations are enabled
If this property is true the treeview will animate expansion and collapsing of branches. If this property is false, the treeview will expand or collapse branches immediately without showing the animation.
By default, this property is false.
This property was introduced in Qt 4.2.
Access functions:
| bool | isAnimated() const |
| void | setAnimated(bool enable) |
autoExpandDelay : int
This property holds the delay time before items in a tree are opened during a drag and drop operation.
This property holds the amount of time in milliseconds that the user must wait over a node before that node will automatically open or close. If the time is set to less then 0 then it will not be activated.
By default, this property has a value of -1, meaning that auto-expansion is disabled.
This property was introduced in Qt 4.3.
Access functions:
| int | autoExpandDelay() const |
| void | setAutoExpandDelay(int delay) |
expandsOnDoubleClick : bool
This property holds whether the items can be expanded by double-clicking.
This property holds whether the user can expand and collapse items by double-clicking. The default value is true.
This property was introduced in Qt 4.4.
Access functions:
| bool | expandsOnDoubleClick() const |
| void | setExpandsOnDoubleClick(bool enable) |
See also itemsExpandable.
headerHidden : bool
This property holds whether the header is shown or not.
If this property is true, the header is not shown otherwise it is. The default value is false.
This property was introduced in Qt 4.4.
Access functions:
| bool | isHeaderHidden() const |
| void | setHeaderHidden(bool hide) |
See also header().
indentation : int
indentation of the items in the tree view.
This property holds the indentation measured in pixels of the items for each level in the tree view. For top-level items, the indentation specifies the horizontal distance from the viewport edge to the items in the first column; for child items, it specifies their indentation from their parent items.
By default, the value of this property is style dependent. Thus, when the style changes, this property updates from it. Calling setIndentation() stops the updates, calling resetIndentation() will restore default behavior.
Access functions:
| int | indentation() const |
| void | setIndentation(int i) |
| void | resetIndentation() |
itemsExpandable : bool
This property holds whether the items are expandable by the user.
This property holds whether the user can expand and collapse items interactively.
By default, this property is true.
Access functions:
| bool | itemsExpandable() const |
| void | setItemsExpandable(bool enable) |
rootIsDecorated : bool
This property holds whether to show controls for expanding and collapsing top-level items
Items with children are typically shown with controls to expand and collapse them, allowing their children to be shown or hidden. If this property is false, these controls are not shown for top-level items. This can be used to make a single level tree structure appear like a simple list of items.
By default, this property is true.
Access functions:
| bool | rootIsDecorated() const |
| void | setRootIsDecorated(bool show) |
sortingEnabled : bool
This property holds whether sorting is enabled
If this property is true, sorting is enabled for the tree; if the property is false, sorting is not enabled. The default value is false.
Note: In order to avoid performance issues, it is recommended that sorting is enabled after inserting the items into the tree. Alternatively, you could also insert the items into a list before inserting the items into the tree.
This property was introduced in Qt 4.2.
Access functions:
| bool | isSortingEnabled() const |
| void | setSortingEnabled(bool enable) |
See also sortByColumn().
uniformRowHeights : bool
This property holds whether all items in the treeview have the same height
This property should only be set to true if it is guaranteed that all items in the view has the same height. This enables the view to do some optimizations.
The height is obtained from the first item in the view. It is updated when the data changes on that item.
Note: If the editor size hint is bigger than the cell size hint, then the size hint of the editor will be used.
By default, this property is false.
Access functions:
| bool | uniformRowHeights() const |
| void | setUniformRowHeights(bool uniform) |
wordWrap : bool
This property holds the item text word-wrapping policy
If this property is true then the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property is false by default.
Note that even if wrapping is enabled, the cell will not be expanded to fit all text. Ellipsis will be inserted according to the current textElideMode.
This property was introduced in Qt 4.3.
Access functions:
| bool | wordWrap() const |
| void | setWordWrap(bool on) |