|  | Home · All Classes · Modules

# QTreeView Class Reference
[QtGui module]

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

Inherits QAbstractItemView.

Inherited by QHelpContentWidget and QTreeWidget.

### Methods

- __init__ (self, QWidget parent = None)
- bool allColumnsShowFocus (self)
- int autoExpandDelay (self)
- collapse (self, QModelIndex index)
- collapseAll (self)
- int columnAt (self, int x)
- columnCountChanged (self, int oldCount, int newCount)
- columnMoved (self)
- columnResized (self, int column, int oldSize, int newSize)
- int columnViewportPosition (self, int column)
- int columnWidth (self, int column)
- currentChanged (self, QModelIndex current, QModelIndex previous)
- dataChanged (self, QModelIndex topLeft, QModelIndex bottomRight)
- doItemsLayout (self)
- dragMoveEvent (self, QDragMoveEvent event)
- drawBranches (self, QPainter painter, QRect rect, QModelIndex index)
- drawRow (self, QPainter painter, QStyleOptionViewItem options, QModelIndex index)
- drawTree (self, QPainter painter, QRegion region)
- expand (self, QModelIndex index)
- expandAll (self)
- bool expandsOnDoubleClick (self)
- expandToDepth (self, int depth)
- QHeaderView header (self)
- hideColumn (self, int column)
- int horizontalOffset (self)
- horizontalScrollbarAction (self, int action)
- int indentation (self)
- QModelIndex indexAbove (self, QModelIndex index)
- QModelIndex indexAt (self, QPoint p)
- QModelIndex indexBelow (self, QModelIndex index)
- int indexRowSizeHint (self, QModelIndex index)
- bool isAnimated (self)
- bool isColumnHidden (self, int column)
- bool isExpanded (self, QModelIndex index)
- bool isFirstColumnSpanned (self, int row, QModelIndex parent)
- bool isHeaderHidden (self)
- bool isIndexHidden (self, QModelIndex index)
- bool isRowHidden (self, int row, QModelIndex parent)
- bool isSortingEnabled (self)
- bool itemsExpandable (self)
- keyboardSearch (self, QString search)
- keyPressEvent (self, QKeyEvent event)
- mouseDoubleClickEvent (self, QMouseEvent e)
- mouseMoveEvent (self, QMouseEvent event)
- mousePressEvent (self, QMouseEvent e)
- mouseReleaseEvent (self, QMouseEvent event)
- QModelIndex moveCursor (self, QAbstractItemView.CursorAction cursorAction, Qt.KeyboardModifiers modifiers)
- paintEvent (self, QPaintEvent e)
- reexpand (self)
- reset (self)
- resizeColumnToContents (self, int column)
- bool rootIsDecorated (self)
- int rowHeight (self, QModelIndex index)
- rowsAboutToBeRemoved (self, QModelIndex parent, int start, int end)
- rowsInserted (self, QModelIndex parent, int start, int end)
- rowsRemoved (self, QModelIndex parent, int first, int last)
- scrollContentsBy (self, int dx, int dy)
- scrollTo (self, QModelIndex index, QAbstractItemView.ScrollHint hint = QAbstractItemView.EnsureVisible)
- selectAll (self)
- QModelIndex-list selectedIndexes (self)
- selectionChanged (self, QItemSelection selected, QItemSelection deselected)
- setAllColumnsShowFocus (self, bool enable)
- setAnimated (self, bool enable)
- setAutoExpandDelay (self, int delay)
- setColumnHidden (self, int column, bool hide)
- setColumnWidth (self, int column, int width)
- setExpanded (self, QModelIndex index, bool expand)
- setExpandsOnDoubleClick (self, bool enable)
- setFirstColumnSpanned (self, int row, QModelIndex parent, bool span)
- setHeader (self, QHeaderView header)
- setHeaderHidden (self, bool hide)
- setIndentation (self, int i)
- setItemsExpandable (self, bool enable)
- setModel (self, QAbstractItemModel model)
- setRootIndex (self, QModelIndex index)
- setRootIsDecorated (self, bool show)
- setRowHidden (self, int row, QModelIndex parent, bool hide)
- setSelection (self, QRect rect, QItemSelectionModel.SelectionFlags command)
- setSelectionModel (self, QItemSelectionModel selectionModel)
- setSortingEnabled (self, bool enable)
- setUniformRowHeights (self, bool uniform)
- setWordWrap (self, bool on)
- showColumn (self, int column)
- int sizeHintForColumn (self, int column)
- sortByColumn (self, int column)
- sortByColumn (self, int column, Qt.SortOrder order)
- timerEvent (self, QTimerEvent event)
- bool uniformRowHeights (self)
- updateGeometries (self)
- int verticalOffset (self)
- bool viewportEvent (self, QEvent event)
- QRect visualRect (self, QModelIndex index)
- QRegion visualRegionForSelection (self, QItemSelection selection)
- bool wordWrap (self)

### Qt Signals

- void collapsed (const QModelIndex&)
- void expanded (const QModelIndex&)

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

## Method Documentation

### QTreeView.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a table view with a parent to represent a model's data. Use setModel() to set the model.

See also QAbstractItemModel.

### bool QTreeView.allColumnsShowFocus (self)

### int QTreeView.autoExpandDelay (self)

### QTreeView.collapse (self, QModelIndex index)

This method is also a Qt slot with the C++ signature `void collapse(const QModelIndex&)`.

Collapses the model item specified by the index.

See also collapsed().

### QTreeView.collapseAll (self)

This method is also a Qt slot with the C++ signature `void collapseAll()`.

Collapses all expanded items.

This function was introduced in Qt 4.2.

See also expandAll(), expand(), collapse(), and setExpanded().

### int QTreeView.columnAt (self, int x)

Returns the column in the tree view whose header covers the x coordinate given.

### QTreeView.columnCountChanged (self, int oldCount, int newCount)

This method is also a Qt slot with the C++ signature `void columnCountChanged(int,int)`.

Informs the tree view that the number of columns in the tree view has changed from oldCount to newCount.

### QTreeView.columnMoved (self)

This method is also a Qt slot with the C++ signature `void columnMoved()`.

This slot is called whenever a column has been moved.

### QTreeView.columnResized (self, int column, int oldSize, int newSize)

This method is also a Qt slot with the C++ signature `void columnResized(int,int,int)`.

This function is called whenever column's size is changed in the header. oldSize and newSize give the previous size and the new size in pixels.

See also setColumnWidth().

### int QTreeView.columnViewportPosition (self, int column)

Returns the horizontal position of the column in the viewport.

### int QTreeView.columnWidth (self, int column)

Returns the width of the column.

See also resizeColumnToContents() and setColumnWidth().

### QTreeView.currentChanged (self, QModelIndex current, QModelIndex previous)

### QTreeView.dataChanged (self, QModelIndex topLeft, QModelIndex bottomRight)

This method is also a Qt slot with the C++ signature `void dataChanged(const QModelIndex&,const QModelIndex&)`.

### QTreeView.doItemsLayout (self)

### QTreeView.dragMoveEvent (self, QDragMoveEvent event)

### QTreeView.drawBranches (self, QPainter painter, QRect rect, QModelIndex index)

Draws the branches in the tree view on the same row as the model item index, using the painter given. The branches are drawn in the rectangle specified by rect.

### QTreeView.drawRow (self, QPainter painter, QStyleOptionViewItem options, QModelIndex index)

Draws the row in the tree view that contains the model item index, using the painter given. The option control how the item is displayed.

See also setAlternatingRowColors().

### QTreeView.drawTree (self, QPainter painter, QRegion region)

Draws the part of the tree intersecting the given region using the specified painter.

This function was introduced in Qt 4.2.

See also paintEvent().

### QTreeView.expand (self, QModelIndex index)

This method is also a Qt slot with the C++ signature `void expand(const QModelIndex&)`.

Expands the model item specified by the index.

See also expanded().

### QTreeView.expandAll (self)

This method is also a Qt slot with the C++ signature `void expandAll()`.

Expands all expandable items.

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

This function was introduced in Qt 4.2.

See also collapseAll(), expand(), collapse(), and setExpanded().

### bool QTreeView.expandsOnDoubleClick (self)

### QTreeView.expandToDepth (self, int depth)

This method is also a Qt slot with the C++ signature `void expandToDepth(int)`.

Expands all expandable items to the given depth.

This function was introduced in Qt 4.3.

See also expandAll(), collapseAll(), expand(), collapse(), and setExpanded().

### QHeaderView QTreeView.header (self)

Returns the header for the tree view.

See also setHeader() and QAbstractItemModel.headerData().

### QTreeView.hideColumn (self, int column)

This method is also a Qt slot with the C++ signature `void hideColumn(int)`.

Hides the column given.

Note: This function should only be called after the model has been initialized, as the view needs to know the number of columns in order to hide column.

See also showColumn() and setColumnHidden().

### int QTreeView.horizontalOffset (self)

Returns the horizontal offset of the items in the treeview.

Note that the tree view uses the horizontal header section positions to determine the positions of columns in the view.

Reimplemented from QAbstractItemView.

See also verticalOffset().

### QTreeView.horizontalScrollbarAction (self, int action)

### int QTreeView.indentation (self)

### QModelIndex QTreeView.indexAbove (self, QModelIndex index)

Returns the model index of the item above index.

### QModelIndex QTreeView.indexAt (self, QPoint p)

### QModelIndex QTreeView.indexBelow (self, QModelIndex index)

Returns the model index of the item below index.

### int QTreeView.indexRowSizeHint (self, QModelIndex index)

Returns the size hint for the row indicated by index.

See also sizeHintForColumn() and uniformRowHeights().

### bool QTreeView.isAnimated (self)

### bool QTreeView.isColumnHidden (self, int column)

Returns true if the column is hidden; otherwise returns false.

See also hideColumn() and isRowHidden().

### bool QTreeView.isExpanded (self, QModelIndex index)

Returns true if the model item index is expanded; otherwise returns false.

See also expand(), expanded(), and setExpanded().

### bool QTreeView.isFirstColumnSpanned (self, int row, QModelIndex parent)

Returns true if the item in first column in the given row of the parent is spanning all the columns; otherwise returns false.

This function was introduced in Qt 4.3.

See also setFirstColumnSpanned().

### bool QTreeView.isHeaderHidden (self)

### bool QTreeView.isIndexHidden (self, QModelIndex index)

### bool QTreeView.isRowHidden (self, int row, QModelIndex parent)

Returns true if the item in the given row of the parent is hidden; otherwise returns false.

See also setRowHidden() and isColumnHidden().

### bool QTreeView.isSortingEnabled (self)

### bool QTreeView.itemsExpandable (self)

### QTreeView.keyboardSearch (self, QString search)

### QTreeView.keyPressEvent (self, QKeyEvent event)

### QTreeView.mouseDoubleClickEvent (self, QMouseEvent e)

### QTreeView.mouseMoveEvent (self, QMouseEvent event)

### QTreeView.mousePressEvent (self, QMouseEvent e)

### QTreeView.mouseReleaseEvent (self, QMouseEvent event)

### QModelIndex QTreeView.moveCursor (self, QAbstractItemView.CursorAction cursorAction, Qt.KeyboardModifiers modifiers)

Move the cursor in the way described by cursorAction, using the information provided by the button modifiers.

Reimplemented from QAbstractItemView.

### QTreeView.paintEvent (self, QPaintEvent e)

### QTreeView.reexpand (self)

This method is also a Qt slot with the C++ signature `void reexpand()`.

### QTreeView.reset (self)

### QTreeView.resizeColumnToContents (self, int column)

This method is also a Qt slot with the C++ signature `void resizeColumnToContents(int)`.

Resizes the column given to the size of its contents.

See also columnWidth() and setColumnWidth().

### bool QTreeView.rootIsDecorated (self)

### int QTreeView.rowHeight (self, QModelIndex index)

Returns the height of the row indicated by the given index.

This function was introduced in Qt 4.3.

See also indexRowSizeHint().

### QTreeView.rowsAboutToBeRemoved (self, QModelIndex parent, int start, int end)

Informs the view that the rows from the start row to the end row inclusive are about to removed from the given parent model item.

Reimplemented from QAbstractItemView.

### QTreeView.rowsInserted (self, QModelIndex parent, int start, int end)

Informs the view that the rows from the start row to the end row inclusive have been inserted into the parent model item.

Reimplemented from QAbstractItemView.

### QTreeView.rowsRemoved (self, QModelIndex parent, int first, int last)

This method is also a Qt slot with the C++ signature `void rowsRemoved(const QModelIndex&,int,int)`.

Informs the view that the rows from the start row to the end row inclusive have been removed from the given parent model item.

This function was introduced in Qt 4.1.

### QTreeView.scrollContentsBy (self, int dx, int dy)

Scrolls the contents of the tree view by (dx, dy).

Reimplemented from QAbstractScrollArea.

### QTreeView.scrollTo (self, QModelIndex index, QAbstractItemView.ScrollHint hint = QAbstractItemView.EnsureVisible)

Scroll the contents of the tree view until the given model item index is visible. The hint parameter specifies more precisely where the item should be located after the operation. If any of the parents of the model item are collapsed, they will be expanded to ensure that the model item is visible.

Reimplemented from QAbstractItemView.

### QTreeView.selectAll (self)

This method is also a Qt slot with the C++ signature `void selectAll()`.

### QModelIndex-list QTreeView.selectedIndexes (self)

### QTreeView.selectionChanged (self, QItemSelection selected, QItemSelection deselected)

### QTreeView.setAllColumnsShowFocus (self, bool enable)

### QTreeView.setAnimated (self, bool enable)

### QTreeView.setAutoExpandDelay (self, int delay)

### QTreeView.setColumnHidden (self, int column, bool hide)

If hide is true the column is hidden, otherwise the column is shown.

See also isColumnHidden(), hideColumn(), and setRowHidden().

### QTreeView.setColumnWidth (self, int column, int width)

Sets the width of the given column to the width specified.

This function was introduced in Qt 4.2.

See also columnWidth() and resizeColumnToContents().

### QTreeView.setExpanded (self, QModelIndex index, bool expand)

Sets the item referred to by index to either collapse or expanded, depending on the value of expanded.

See also expanded(), expand(), and isExpanded().

### QTreeView.setExpandsOnDoubleClick (self, bool enable)

### QTreeView.setFirstColumnSpanned (self, int row, QModelIndex parent, bool span)

If span is true the item in the first column in the row with the given parent is set to span all columns, otherwise all items on the row are shown.

This function was introduced in Qt 4.3.

See also isFirstColumnSpanned().

### QTreeView.setHeader (self, QHeaderView header)

Sets the header for the tree view, to the given header.

The view takes ownership over the given header and deletes it when a new header is set.

See also QAbstractItemModel.headerData().

### QTreeView.setHeaderHidden (self, bool hide)

### QTreeView.setIndentation (self, int i)

### QTreeView.setItemsExpandable (self, bool enable)

### QTreeView.setModel (self, QAbstractItemModel model)

### QTreeView.setRootIndex (self, QModelIndex index)

### QTreeView.setRootIsDecorated (self, bool show)

### QTreeView.setRowHidden (self, int row, QModelIndex parent, bool hide)

If hide is true the row with the given parent is hidden, otherwise the row is shown.

See also isRowHidden() and setColumnHidden().

### QTreeView.setSelection (self, QRect rect, QItemSelectionModel.SelectionFlags command)

Applies the selection command to the items in or touched by the rectangle, rect.

Reimplemented from QAbstractItemView.

See also selectionCommand().

### QTreeView.setSelectionModel (self, QItemSelectionModel selectionModel)

### QTreeView.setSortingEnabled (self, bool enable)

### QTreeView.setUniformRowHeights (self, bool uniform)

### QTreeView.setWordWrap (self, bool on)

### QTreeView.showColumn (self, int column)

This method is also a Qt slot with the C++ signature `void showColumn(int)`.

Shows the given column in the tree view.

See also hideColumn() and setColumnHidden().

### int QTreeView.sizeHintForColumn (self, int column)

Returns the size hint for the column's width or -1 if there is no model.

If you need to set the width of a given column to a fixed value, call QHeaderView.resizeSection() on the view's header.

If you reimplement this function in a subclass, note that the value you return is only used when resizeColumnToContents() is called. In that case, if a larger column width is required by either the view's header or the item delegate, that width will be used instead.

Reimplemented from QAbstractItemView.

See also QWidget.sizeHint and header().

### QTreeView.sortByColumn (self, int column)

This method is also a Qt slot with the C++ signature `void sortByColumn(int)`.

Sets the model up for sorting by the values in the given column and order.

This function was introduced in Qt 4.2.

See also sortingEnabled.

### QTreeView.sortByColumn (self, int column, Qt.SortOrder order)

### QTreeView.timerEvent (self, QTimerEvent event)

### bool QTreeView.uniformRowHeights (self)

### QTreeView.updateGeometries (self)

### int QTreeView.verticalOffset (self)

Returns the vertical offset of the items in the tree view.

Reimplemented from QAbstractItemView.

See also horizontalOffset().

### bool QTreeView.viewportEvent (self, QEvent event)

### QRect QTreeView.visualRect (self, QModelIndex index)

Returns the rectangle on the viewport occupied by the item at index. If the index is not visible or explicitly hidden, the returned rectangle is invalid.

Reimplemented from QAbstractItemView.

### QRegion QTreeView.visualRegionForSelection (self, QItemSelection selection)

Returns the rectangle from the viewport of the items in the given selection.

Reimplemented from QAbstractItemView.

### bool QTreeView.wordWrap (self)

## Qt Signal Documentation

### void collapsed (const QModelIndex&)

This signal is emitted when the item specified by index is collapsed.

### void expanded (const QModelIndex&)

This signal is emitted when the item specified by index is expanded.

See also setExpanded().