PySide6.QtWidgets.QTreeView¶
- class QTreeView¶
The
QTreeViewclass provides a default model/view implementation of a tree view. More…Inherited by:
QTreeWidget,QHelpContentWidgetSynopsis¶
Properties¶
allColumnsShowFocusᅟ- Whether items should show keyboard focus using all columnsanimatedᅟ- Whether animations are enabledautoExpandDelayᅟ- Delay time before items in a tree are opened during a drag and drop operationexpandsOnDoubleClickᅟ- Whether the items can be expanded by double-clickingheaderHiddenᅟ- Whether the header is shown or notindentationᅟ- Of the items in the tree viewitemsExpandableᅟ- Whether the items are expandable by the userrootIsDecoratedᅟ- Whether to show controls for expanding and collapsing top-level itemssortingEnabledᅟ- Whether sorting is enableduniformRowHeightsᅟ- Whether all items in the treeview have the same heightwordWrapᅟ- The item text word-wrapping policy
Methods¶
def
__init__()def
columnAt()def
columnWidth()def
drawTree()def
header()def
indentation()def
indexAbove()def
indexBelow()def
isAnimated()def
isColumnHidden()def
isExpanded()def
isHeaderHidden()def
isRowHidden()def
rowHeight()def
setAnimated()def
setColumnWidth()def
setExpanded()def
setHeader()def
setIndentation()def
setRowHidden()def
setWordWrap()def
treePosition()def
wordWrap()
Virtual methods¶
def
drawBranches()def
drawRow()
Slots¶
def
collapse()def
collapseAll()def
columnMoved()def
columnResized()def
expand()def
expandAll()def
expandToDepth()def
hideColumn()def
reexpand()def
rowsRemoved()def
showColumn()def
sortByColumn()
Signals¶
def
collapsed()def
expanded()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
A
QTreeViewimplements a tree representation of items from a model. This class is used to provide standard hierarchical lists that were previously provided by theQListViewclass, but using the more flexible approach provided by Qt’s model/view architecture.The
QTreeViewclass is one of the Model/View Classes and is part of Qt’s model/view framework .QTreeViewimplements the interfaces defined by theQAbstractItemViewclass 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()) //! [0] //! [1] if __name__ == "__main__": app = QApplication([]) splitter = QSplitter() 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()orexpanded()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
indentationproperty.Headers in tree views are constructed using the
QHeaderViewclass and can be hidden usingheader()->hide(). Note that each header is configured with itsstretchLastSectionproperty 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‘ssetSectionsMovable()function. For more information about rearranging sections, seeMoving Header Sections.Key Bindings¶
QTreeViewsupports 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
uniformRowHeightsproperty to true.Note
Properties can be used directly when
from __feature__ import true_propertyis used or via accessor functions otherwise.- property allColumnsShowFocusᅟ: bool¶
This property holds whether items should show keyboard focus using all columns.
If this property is
trueall columns will show focus, otherwise only one column will show focus.The default is false.
- Access functions:
- property animatedᅟ: bool¶
This property holds whether animations are enabled.
If this property is
truethe treeview will animate expansion and collapsing of branches. If this property isfalse, the treeview will expand or collapse branches immediately without showing the animation.By default, this property is
false.- Access functions:
- property 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. 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.
- Access functions:
- property 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.
See also
- Access functions:
- property 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.See also
- Access functions:
- property indentationᅟ: 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, 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:
- property 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:
- property 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:
- property 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.
See also
- Access functions:
- property 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:
- property wordWrapᅟ: bool¶
This property holds the item text word-wrapping policy.
If this property is
truethen the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property isfalseby 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.- Access functions:
Constructs a tree view with a
parentto represent a model’s data. UsesetModel()to set the model.See also
- allColumnsShowFocus()¶
- Return type:
bool
See also
Getter of property
allColumnsShowFocusᅟ.- autoExpandDelay()¶
- Return type:
int
See also
Getter of property
autoExpandDelayᅟ.- collapse(index)¶
- Parameters:
index –
QModelIndex
Collapses the model item specified by the
index.See also
- collapseAll()¶
Collapses all expanded items.
See also
- collapsed(index)¶
- Parameters:
index –
QModelIndex
This signal is emitted when the item specified by
indexis collapsed.- columnAt(x)¶
- Parameters:
x – int
- Return type:
int
Returns the column in the tree view whose header covers the
xcoordinate given.- columnCountChanged(oldCount, newCount)¶
- Parameters:
oldCount – int
newCount – int
Informs the tree view that the number of columns in the tree view has changed from
oldCounttonewCount.- columnMoved()¶
This slot is called whenever a column has been moved.
- columnResized(column, oldSize, newSize)¶
- Parameters:
column – int
oldSize – int
newSize – int
This function is called whenever
column's size is changed in the header.oldSizeandnewSizegive the previous size and the new size in pixels.See also
- columnViewportPosition(column)¶
- Parameters:
column – int
- Return type:
int
Returns the horizontal position of the
columnin the viewport.- columnWidth(column)¶
- Parameters:
column – int
- Return type:
int
Returns the width of the
column.See also
- drawBranches(painter, rect, index)¶
- Parameters:
painter –
QPainterrect –
QRectindex –
QModelIndex
Draws the branches in the tree view on the same row as the model item
index, using thepaintergiven. The branches are drawn in the rectangle specified byrect.- drawRow(painter, options, index)¶
- Parameters:
painter –
QPainteroptions –
QStyleOptionViewItemindex –
QModelIndex
Draws the row in the tree view that contains the model item
index, using thepaintergiven. Theoptioncontrols how the item is displayed.See also
setAlternatingRowColors()Draws the part of the tree intersecting the given
regionusing the specifiedpainter.See also
paintEvent()- expand(index)¶
- Parameters:
index –
QModelIndex
Expands the model item specified by the
index.See also
- expandAll()¶
Expands all expandable items.
Note
This function will not try to fetch more data.
Warning
If the model contains a large number of items, this function will take some time to execute.
See also
- expandRecursively(index[, depth=-1])¶
- Parameters:
index –
QModelIndexdepth – int
Expands the item at the given
indexand all its children to the givendepth. Thedepthis relative to the givenindex. Adepthof -1 will expand all children, adepthof 0 will only expand the givenindex.Note
This function will not try to fetch more data.
Warning
If the model contains a large number of items, this function will take some time to execute.
See also
- expandToDepth(depth)¶
- Parameters:
depth – int
Expands all expandable items to the given
depth.Note
This function will not try to fetch more data.
- expanded(index)¶
- Parameters:
index –
QModelIndex
This signal is emitted when the item specified by
indexis expanded.See also
- expandsOnDoubleClick()¶
- Return type:
bool
See also
Getter of property
expandsOnDoubleClickᅟ.- header()¶
- Return type:
Returns the header for the tree view.
See also
- hideColumn(column)¶
- Parameters:
column – int
Hides the
columngiven.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
- indentation()¶
- Return type:
int
See also
Getter of property
indentationᅟ.- indexAbove(index)¶
- Parameters:
index –
QModelIndex- Return type:
Returns the model index of the item above
index.- indexBelow(index)¶
- Parameters:
index –
QModelIndex- Return type:
Returns the model index of the item below
index.- indexRowSizeHint(index)¶
- Parameters:
index –
QModelIndex- Return type:
int
Returns the size hint for the row indicated by
index.See also
sizeHintForColumn()uniformRowHeights()- isAnimated()¶
- Return type:
bool
Getter of property
animatedᅟ.- isColumnHidden(column)¶
- Parameters:
column – int
- Return type:
bool
Returns
trueif thecolumnis hidden; otherwise returnsfalse.See also
- isExpanded(index)¶
- Parameters:
index –
QModelIndex- Return type:
bool
Returns
trueif the model itemindexis expanded; otherwise returns false.See also
- isFirstColumnSpanned(row, parent)¶
- Parameters:
row – int
parent –
QModelIndex
- Return type:
bool
Returns
trueif the item in first column in the givenrowof theparentis spanning all the columns; otherwise returnsfalse.See also
- isHeaderHidden()¶
- Return type:
bool
Getter of property
headerHiddenᅟ.- isRowHidden(row, parent)¶
- Parameters:
row – int
parent –
QModelIndex
- Return type:
bool
Returns
trueif the item in the givenrowof theparentis hidden; otherwise returnsfalse.See also
- isSortingEnabled()¶
- Return type:
bool
Getter of property
sortingEnabledᅟ.- itemsExpandable()¶
- Return type:
bool
See also
Getter of property
itemsExpandableᅟ.- reexpand()¶
- resetIndentation()¶
Reset function of property
indentationᅟ.- resizeColumnToContents(column)¶
- Parameters:
column – int
Resizes the
columngiven to the size of its contents.See also
columnWidth()setColumnWidth()sizeHintForColumn()resizeContentsPrecision()- rootIsDecorated()¶
- Return type:
bool
See also
Getter of property
rootIsDecoratedᅟ.- rowHeight(index)¶
- Parameters:
index –
QModelIndex- Return type:
int
Returns the height of the row indicated by the given
index.See also
- rowsRemoved(parent, first, last)¶
- Parameters:
parent –
QModelIndexfirst – int
last – int
Informs the view that the rows from the
startrow to theendrow inclusive have been removed from the givenparentmodel item.- setAllColumnsShowFocus(enable)¶
- Parameters:
enable – bool
See also
Setter of property
allColumnsShowFocusᅟ.- setAnimated(enable)¶
- Parameters:
enable – bool
See also
Setter of property
animatedᅟ.- setAutoExpandDelay(delay)¶
- Parameters:
delay – int
See also
Setter of property
autoExpandDelayᅟ.- setColumnHidden(column, hide)¶
- Parameters:
column – int
hide – bool
If
hideis true thecolumnis hidden, otherwise thecolumnis shown.See also
- setColumnWidth(column, width)¶
- Parameters:
column – int
width – int
Sets the width of the given
columnto thewidthspecified.See also
- setExpanded(index, expand)¶
- Parameters:
index –
QModelIndexexpand – bool
Sets the item referred to by
indexto either collapse or expanded, depending on the value ofexpanded.See also
- setExpandsOnDoubleClick(enable)¶
- Parameters:
enable – bool
See also
Setter of property
expandsOnDoubleClickᅟ.- setFirstColumnSpanned(row, parent, span)¶
- Parameters:
row – int
parent –
QModelIndexspan – bool
If
spanis true the item in the first column in therowwith the givenparentis set to span all columns, otherwise all items on theroware shown.See also
- setHeader(header)¶
- Parameters:
header –
QHeaderView
Sets the header for the tree view, to the given
header.The view takes ownership over the given
headerand deletes it when a new header is set.See also
- setHeaderHidden(hide)¶
- Parameters:
hide – bool
See also
Setter of property
headerHiddenᅟ.- setIndentation(i)¶
- Parameters:
i – int
See also
Setter of property
indentationᅟ.- setItemsExpandable(enable)¶
- Parameters:
enable – bool
See also
Setter of property
itemsExpandableᅟ.- setRootIsDecorated(show)¶
- Parameters:
show – bool
See also
Setter of property
rootIsDecoratedᅟ.- setRowHidden(row, parent, hide)¶
- Parameters:
row – int
parent –
QModelIndexhide – bool
If
hideis true therowwith the givenparentis hidden, otherwise therowis shown.See also
- setSortingEnabled(enable)¶
- Parameters:
enable – bool
See also
Setter of property
sortingEnabledᅟ.- setTreePosition(logicalIndex)¶
- Parameters:
logicalIndex – int
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.See also
- setUniformRowHeights(uniform)¶
- Parameters:
uniform – bool
See also
Setter of property
uniformRowHeightsᅟ.- setWordWrap(on)¶
- Parameters:
on – bool
See also
Setter of property
wordWrapᅟ.- showColumn(column)¶
- Parameters:
column – int
Shows the given
columnin the tree view.See also
Sorts the model by the values in the given
columnandorder.columnmay 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- 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.
See also
- uniformRowHeights()¶
- Return type:
bool
See also
Getter of property
uniformRowHeightsᅟ.- wordWrap()¶
- Return type:
bool
See also
Getter of property
wordWrapᅟ.