|  | Home · Overviews · Examples

# QTreeView Class Reference
[com.trolltech.qt.gui module]

The `QTreeView` class provides a default model/view implementation of a tree view. More...

Inherits `QAbstractItemView`.

Inherited by `QTreeWidget`.

## Detailed Description

The `QTreeView` class provides a default model/view implementation of a tree view.

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 `QDirModel` and displayed as a tree:

            QDirModel *model = new QDirModel;
            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.

### 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
--- | ---
UpArrow | 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.
DownArrow | 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.
LeftArrow | Hides the children of the current item (if present) by collapsing a branch.
Minus | Same as LeftArrow.
RightArrow | Reveals the children of the current item (if present) by expanding a branch.
Plus | Same as RightArrow.
Asterisk | Expands all children of the current item (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.

A Windows XP style tree view. | A Macintosh style tree view. | A Plastique style tree view.

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