QTreeView

Inheritance diagram of QTreeView

Inherited by: QHelpContentWidget, QTreeWidget

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

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

A PySide.QtGui.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 PySide.QtGui.QTreeView class is one of the Model/View Classes and is part of Qt’s model/view framework .

PySide.QtGui.QTreeView implements the interfaces defined by the PySide.QtGui.QAbstractItemView class to allow it to display data provided by models derived from the PySide.QtCore.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 PySide.QtGui.QDirModel 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 PySide.QtGui.QTreeView.collapsed() or PySide.QtGui.QTreeView.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 PySide.QtGui.QTreeView.indentation() property.

Headers in tree views are constructed using the PySide.QtGui.QHeaderView class and can be hidden using header()->hide() . Note that each header is configured with its PySide.QtGui.QHeaderView.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.

Key Bindings

PySide.QtGui.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 LeftArrow.
Right 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.
../../_images/windowsxp-treeview.png ../../_images/macintosh-treeview.png ../../_images/plastique-treeview.png
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 PySide.QtGui.QTreeView.uniformRowHeights() property to true.

class PySide.QtGui.QTreeView([parent=None])
Parameters:parentPySide.QtGui.QWidget

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

PySide.QtGui.QTreeView.allColumnsShowFocus()
Return type:PySide.QtCore.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.

PySide.QtGui.QTreeView.autoExpandDelay()
Return type:PySide.QtCore.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.

PySide.QtGui.QTreeView.collapse(index)
Parameters:indexPySide.QtCore.QModelIndex

Collapses the model item specified by the index .

PySide.QtGui.QTreeView.collapseAll()

Collapses all expanded items.

PySide.QtGui.QTreeView.collapsed(index)
Parameters:indexPySide.QtCore.QModelIndex
PySide.QtGui.QTreeView.columnAt(x)
Parameters:xPySide.QtCore.int
Return type:PySide.QtCore.int

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

PySide.QtGui.QTreeView.columnCountChanged(oldCount, newCount)
Parameters:
  • oldCountPySide.QtCore.int
  • newCountPySide.QtCore.int

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

PySide.QtGui.QTreeView.columnMoved()

This slot is called whenever a column has been moved.

PySide.QtGui.QTreeView.columnResized(column, oldSize, newSize)
Parameters:
  • columnPySide.QtCore.int
  • oldSizePySide.QtCore.int
  • newSizePySide.QtCore.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.

PySide.QtGui.QTreeView.columnViewportPosition(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtCore.int

Returns the horizontal position of the column in the viewport.

PySide.QtGui.QTreeView.columnWidth(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtCore.int

Returns the width of the column .

PySide.QtGui.QTreeView.drawBranches(painter, rect, index)
Parameters:

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 .

PySide.QtGui.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 control how the item is displayed.

PySide.QtGui.QTreeView.drawTree(painter, region)
Parameters:

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

See also

PySide.QtGui.QTreeView.paintEvent()

PySide.QtGui.QTreeView.expand(index)
Parameters:indexPySide.QtCore.QModelIndex

Expands the model item specified by the index .

PySide.QtGui.QTreeView.expandAll()

Expands all expandable items.

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

PySide.QtGui.QTreeView.expandToDepth(depth)
Parameters:depthPySide.QtCore.int

Expands all expandable items to the given depth .

PySide.QtGui.QTreeView.expanded(index)
Parameters:indexPySide.QtCore.QModelIndex
PySide.QtGui.QTreeView.expandsOnDoubleClick()
Return type:PySide.QtCore.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.

PySide.QtGui.QTreeView.header()
Return type:PySide.QtGui.QHeaderView

Returns the header for the tree view.

See also

PySide.QtGui.QTreeView.setHeader() QAbstractItemModel.headerData()

PySide.QtGui.QTreeView.hideColumn(column)
Parameters:columnPySide.QtCore.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 .

PySide.QtGui.QTreeView.indentation()
Return type:PySide.QtCore.int

This property holds 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, this property has a value of 20.

PySide.QtGui.QTreeView.indexAbove(index)
Parameters:indexPySide.QtCore.QModelIndex
Return type:PySide.QtCore.QModelIndex

Returns the model index of the item above index .

PySide.QtGui.QTreeView.indexBelow(index)
Parameters:indexPySide.QtCore.QModelIndex
Return type:PySide.QtCore.QModelIndex

Returns the model index of the item below index .

PySide.QtGui.QTreeView.indexRowSizeHint(index)
Parameters:indexPySide.QtCore.QModelIndex
Return type:PySide.QtCore.int

Returns the size hint for the row indicated by index .

See also

PySide.QtGui.QTreeView.sizeHintForColumn() PySide.QtGui.QTreeView.uniformRowHeights()

PySide.QtGui.QTreeView.isAnimated()
Return type:PySide.QtCore.bool

This property holds whether animations are enabled.

If this property is true the treeview will animate expandsion and collasping 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.

PySide.QtGui.QTreeView.isColumnHidden(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtCore.bool

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

PySide.QtGui.QTreeView.isExpanded(index)
Parameters:indexPySide.QtCore.QModelIndex
Return type:PySide.QtCore.bool

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

PySide.QtGui.QTreeView.isFirstColumnSpanned(row, parent)
Parameters:
Return type:

PySide.QtCore.bool

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

PySide.QtGui.QTreeView.isHeaderHidden()
Return type:PySide.QtCore.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.

PySide.QtGui.QTreeView.isRowHidden(row, parent)
Parameters:
Return type:

PySide.QtCore.bool

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

PySide.QtGui.QTreeView.isSortingEnabled()
Return type:PySide.QtCore.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.

PySide.QtGui.QTreeView.itemsExpandable()
Return type:PySide.QtCore.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.

PySide.QtGui.QTreeView.reexpand()
PySide.QtGui.QTreeView.resizeColumnToContents(column)
Parameters:columnPySide.QtCore.int

Resizes the column given to the size of its contents.

PySide.QtGui.QTreeView.rootIsDecorated()
Return type:PySide.QtCore.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.

PySide.QtGui.QTreeView.rowHeight(index)
Parameters:indexPySide.QtCore.QModelIndex
Return type:PySide.QtCore.int

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

PySide.QtGui.QTreeView.rowsRemoved(parent, first, last)
Parameters:

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

PySide.QtGui.QTreeView.setAllColumnsShowFocus(enable)
Parameters:enablePySide.QtCore.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.

PySide.QtGui.QTreeView.setAnimated(enable)
Parameters:enablePySide.QtCore.bool

This property holds whether animations are enabled.

If this property is true the treeview will animate expandsion and collasping 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.

PySide.QtGui.QTreeView.setAutoExpandDelay(delay)
Parameters:delayPySide.QtCore.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.

PySide.QtGui.QTreeView.setColumnHidden(column, hide)
Parameters:
  • columnPySide.QtCore.int
  • hidePySide.QtCore.bool

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

PySide.QtGui.QTreeView.setColumnWidth(column, width)
Parameters:
  • columnPySide.QtCore.int
  • widthPySide.QtCore.int

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

PySide.QtGui.QTreeView.setExpanded(index, expand)
Parameters:

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

PySide.QtGui.QTreeView.setExpandsOnDoubleClick(enable)
Parameters:enablePySide.QtCore.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.

PySide.QtGui.QTreeView.setFirstColumnSpanned(row, parent, span)
Parameters:

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.

PySide.QtGui.QTreeView.setHeader(header)
Parameters:headerPySide.QtGui.QHeaderView

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

PySide.QtGui.QTreeView.setHeaderHidden(hide)
Parameters:hidePySide.QtCore.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.

PySide.QtGui.QTreeView.setIndentation(i)
Parameters:iPySide.QtCore.int

This property holds 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, this property has a value of 20.

PySide.QtGui.QTreeView.setItemsExpandable(enable)
Parameters:enablePySide.QtCore.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.

PySide.QtGui.QTreeView.setRootIsDecorated(show)
Parameters:showPySide.QtCore.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.

PySide.QtGui.QTreeView.setRowHidden(row, parent, hide)
Parameters:

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

PySide.QtGui.QTreeView.setSortingEnabled(enable)
Parameters:enablePySide.QtCore.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.

PySide.QtGui.QTreeView.setUniformRowHeights(uniform)
Parameters:uniformPySide.QtCore.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.

By default, this property is false.

PySide.QtGui.QTreeView.setWordWrap(on)
Parameters:onPySide.QtCore.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 PySide.QtGui.QAbstractItemView.textElideMode() .

PySide.QtGui.QTreeView.showColumn(column)
Parameters:columnPySide.QtCore.int

Shows the given column in the tree view.

PySide.QtGui.QTreeView.sortByColumn(column, order)
Parameters:
  • columnPySide.QtCore.int
  • orderPySide.QtCore.Qt.SortOrder
PySide.QtGui.QTreeView.uniformRowHeights()
Return type:PySide.QtCore.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.

By default, this property is false.

PySide.QtGui.QTreeView.visualIndex(index)
Parameters:indexPySide.QtCore.QModelIndex
Return type:PySide.QtCore.int
PySide.QtGui.QTreeView.wordWrap()
Return type:PySide.QtCore.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 PySide.QtGui.QAbstractItemView.textElideMode() .