QTreeView

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

Inheritance diagram of PySide2.QtWidgets.QTreeView

Inherited by: QHelpContentWidget, QTreeWidget

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

../../_images/windows-treeview.png

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:

model = QFileSystemModel()
model.setRootPath(QDir.currentPath())

tree =  QTreeView()
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.

class QTreeView([parent=None])
param parent

QWidget

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

PySide2.QtWidgets.QTreeView.allColumnsShowFocus()
Return type

bool

PySide2.QtWidgets.QTreeView.autoExpandDelay()
Return type

int

PySide2.QtWidgets.QTreeView.collapse(index)
Parameters

indexQModelIndex

Collapses the model item specified by the index .

See also

collapsed()

PySide2.QtWidgets.QTreeView.collapseAll()

Collapses all expanded items.

PySide2.QtWidgets.QTreeView.collapsed(index)
Parameters

indexQModelIndex

PySide2.QtWidgets.QTreeView.columnAt(x)
Parameters

xint

Return type

int

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

PySide2.QtWidgets.QTreeView.columnCountChanged(oldCount, newCount)
Parameters
  • oldCountint

  • newCountint

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

PySide2.QtWidgets.QTreeView.columnMoved()

This slot is called whenever a column has been moved.

PySide2.QtWidgets.QTreeView.columnResized(column, oldSize, newSize)
Parameters
  • columnint

  • oldSizeint

  • newSizeint

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()

PySide2.QtWidgets.QTreeView.columnViewportPosition(column)
Parameters

columnint

Return type

int

Returns the horizontal position of the column in the viewport.

PySide2.QtWidgets.QTreeView.columnWidth(column)
Parameters

columnint

Return type

int

Returns the width of the column .

PySide2.QtWidgets.QTreeView.drawBranches(painter, rect, index)
Parameters
  • painterQPainter

  • rectQRect

  • indexQModelIndex

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 .

PySide2.QtWidgets.QTreeView.drawRow(painter, options, index)
Parameters

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

PySide2.QtWidgets.QTreeView.drawTree(painter, region)
Parameters
  • painterQPainter

  • regionQRegion

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

See also

paintEvent()

PySide2.QtWidgets.QTreeView.expand(index)
Parameters

indexQModelIndex

Expands the model item specified by the index .

See also

expanded()

PySide2.QtWidgets.QTreeView.expandAll()

Expands all expandable items.

Warning

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

PySide2.QtWidgets.QTreeView.expandRecursively(index[, depth=-1])
Parameters
  • indexQModelIndex

  • depthint

Expands the item at the given index and all its children to the given depth . The depth is relative to the given index . A depth of -1 will expand all children, a depth of 0 will only expand the given index .

Warning

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

See also

expandAll()

PySide2.QtWidgets.QTreeView.expandToDepth(depth)
Parameters

depthint

Expands all expandable items to the given depth .

PySide2.QtWidgets.QTreeView.expanded(index)
Parameters

indexQModelIndex

PySide2.QtWidgets.QTreeView.expandsOnDoubleClick()
Return type

bool

PySide2.QtWidgets.QTreeView.header()
Return type

QHeaderView

Returns the header for the tree view.

See also

setHeader() headerData()

PySide2.QtWidgets.QTreeView.hideColumn(column)
Parameters

columnint

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 .

PySide2.QtWidgets.QTreeView.indentation()
Return type

int

See also

setIndentation()

PySide2.QtWidgets.QTreeView.indexAbove(index)
Parameters

indexQModelIndex

Return type

QModelIndex

Returns the model index of the item above index .

PySide2.QtWidgets.QTreeView.indexBelow(index)
Parameters

indexQModelIndex

Return type

QModelIndex

Returns the model index of the item below index .

PySide2.QtWidgets.QTreeView.indexRowSizeHint(index)
Parameters

indexQModelIndex

Return type

int

Returns the size hint for the row indicated by index .

See also

sizeHintForColumn() uniformRowHeights()

PySide2.QtWidgets.QTreeView.isAnimated()
Return type

bool

PySide2.QtWidgets.QTreeView.isColumnHidden(column)
Parameters

columnint

Return type

bool

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

PySide2.QtWidgets.QTreeView.isExpanded(index)
Parameters

indexQModelIndex

Return type

bool

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

PySide2.QtWidgets.QTreeView.isFirstColumnSpanned(row, parent)
Parameters
  • rowint

  • parentQModelIndex

Return type

bool

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

PySide2.QtWidgets.QTreeView.isHeaderHidden()
Return type

bool

PySide2.QtWidgets.QTreeView.isRowHidden(row, parent)
Parameters
  • rowint

  • parentQModelIndex

Return type

bool

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

PySide2.QtWidgets.QTreeView.isSortingEnabled()
Return type

bool

PySide2.QtWidgets.QTreeView.itemsExpandable()
Return type

bool

PySide2.QtWidgets.QTreeView.reexpand()
PySide2.QtWidgets.QTreeView.resetIndentation()
PySide2.QtWidgets.QTreeView.resizeColumnToContents(column)
Parameters

columnint

Resizes the column given to the size of its contents.

PySide2.QtWidgets.QTreeView.rootIsDecorated()
Return type

bool

PySide2.QtWidgets.QTreeView.rowHeight(index)
Parameters

indexQModelIndex

Return type

int

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

PySide2.QtWidgets.QTreeView.rowsRemoved(parent, first, last)
Parameters
  • parentQModelIndex

  • firstint

  • lastint

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

PySide2.QtWidgets.QTreeView.setAllColumnsShowFocus(enable)
Parameters

enablebool

PySide2.QtWidgets.QTreeView.setAnimated(enable)
Parameters

enablebool

See also

isAnimated()

PySide2.QtWidgets.QTreeView.setAutoExpandDelay(delay)
Parameters

delayint

PySide2.QtWidgets.QTreeView.setColumnHidden(column, hide)
Parameters
  • columnint

  • hidebool

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

PySide2.QtWidgets.QTreeView.setColumnWidth(column, width)
Parameters
  • columnint

  • widthint

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

PySide2.QtWidgets.QTreeView.setExpanded(index, expand)
Parameters
  • indexQModelIndex

  • expandbool

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

PySide2.QtWidgets.QTreeView.setExpandsOnDoubleClick(enable)
Parameters

enablebool

PySide2.QtWidgets.QTreeView.setFirstColumnSpanned(row, parent, span)
Parameters
  • rowint

  • parentQModelIndex

  • spanbool

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.

PySide2.QtWidgets.QTreeView.setHeader(header)
Parameters

headerQHeaderView

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

headerData()

PySide2.QtWidgets.QTreeView.setHeaderHidden(hide)
Parameters

hidebool

See also

isHeaderHidden()

PySide2.QtWidgets.QTreeView.setIndentation(i)
Parameters

iint

See also

indentation()

PySide2.QtWidgets.QTreeView.setItemsExpandable(enable)
Parameters

enablebool

PySide2.QtWidgets.QTreeView.setRootIsDecorated(show)
Parameters

showbool

PySide2.QtWidgets.QTreeView.setRowHidden(row, parent, hide)
Parameters
  • rowint

  • parentQModelIndex

  • hidebool

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

PySide2.QtWidgets.QTreeView.setSortingEnabled(enable)
Parameters

enablebool

PySide2.QtWidgets.QTreeView.setTreePosition(logicalIndex)
Parameters

logicalIndexint

This specifies that the tree structure should be placed at logical index index . If set to -1 then the tree will always follow visual index 0.

PySide2.QtWidgets.QTreeView.setUniformRowHeights(uniform)
Parameters

uniformbool

PySide2.QtWidgets.QTreeView.setWordWrap(on)
Parameters

onbool

See also

wordWrap()

PySide2.QtWidgets.QTreeView.showColumn(column)
Parameters

columnint

Shows the given column in the tree view.

PySide2.QtWidgets.QTreeView.sortByColumn(column)
Parameters

columnint

Note

This function is deprecated.

This is an overloaded function.

This function is deprecated. Use sortByColumn (int column, SortOrder order) instead. Sorts the model by the values in the given column .

PySide2.QtWidgets.QTreeView.sortByColumn(column, order)
Parameters
  • columnint

  • orderSortOrder

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

column may be -1, in which case no sort indicator will be shown and the model will return to its natural, unsorted order. Note that not all models support this and may even crash in this case.

See also

sortingEnabled

PySide2.QtWidgets.QTreeView.treePosition()
Return type

int

Return the logical index the tree is set on. If the return value is -1 then the tree is placed on the visual index 0.

PySide2.QtWidgets.QTreeView.uniformRowHeights()
Return type

bool

PySide2.QtWidgets.QTreeView.wordWrap()
Return type

bool

See also

setWordWrap()