内容表

上一话题

QToolTip

下一话题

QTreeWidget

QTreeView

QTreeView class provides a default model/view implementation of a tree view. 更多

Inheritance diagram of PySide2.QtWidgets.QTreeView

继承者: QHelpContentWidget , QTreeWidget

概要

函数

虚函数

信号

详细描述

../../_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.

QTreeView class is one of the 模型/视图类 and is part of Qt’s 模型/视图框架 .

QTreeView implements the interfaces defined by the QAbstractItemView 类以允许它显示提供数据,通过模型派生自 QAbstractItemModel 类。

构建树视图以显示来自模型的数据很简单。在以下范例中,目录内容的提供通过 QFileSystemModel 并作为树显示:

model = QFileSystemModel()
model.setRootPath(QDir.currentPath())
tree =  QTreeView()
tree.setModel(model)
											

模型/视图体系结构确保树视图内容随模型改变更新。

拥有子级的项可以处于展开 (子级可见) 或折叠 (子级被隐藏) 状态。当此状态改变 collapsed() or expanded() signal is emitted with the model index of the relevant item.

用于指示层次结构级别缩进量的控制通过 indentation 特性。

树视图 Header 头部的构造使用 QHeaderView 类且可以被隐藏使用 header()->hide() 。注意,每个 Header 头部的配置采用其 stretchLastSection 特性被设为 true,确保视图不浪费为 Header 头部赋值的任何空间。若此值被设为 true,此特性将覆盖 Header 头部最后区间设置的重置尺寸模式。

默认情况下,树视图的所有列是可移动的,除第一列外。要禁用这些列的移动,使用 QHeaderView ‘s setSectionsMovable() 函数。有关重新排列区间的更多信息,见 Moving Header Sections .

键绑定

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

将光标移动到上一行相同列项。若当前项父级没有更多行可导航,光标将移动到父级之前最后同级行相关项。

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

通过折叠分支隐藏当前项 (若存在) 子级。

Minus

如同 Left。

Right

通过展开分支展现当前项 (若存在) 子级。

Plus

如同 Right。

星号

展开当前项及其所有子级 (若存在)。

PageUp

上移光标一页。

PageDown

下移光标一页。

首页

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.

改善性能

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

构造树视图采用 parent to represent a model’s data. Use setModel() to set the model.

PySide2.QtWidgets.QTreeView. allColumnsShowFocus ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. autoExpandDelay ( )
返回类型

int

PySide2.QtWidgets.QTreeView. collapse ( index )
参数

index QModelIndex

折叠的模型项指定通过 index .

另请参阅

collapsed()

PySide2.QtWidgets.QTreeView. collapseAll ( )

折叠所有展开项。

PySide2.QtWidgets.QTreeView. collapsed ( index )
参数

index QModelIndex

PySide2.QtWidgets.QTreeView. columnAt ( x )
参数

x int

返回类型

int

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

PySide2.QtWidgets.QTreeView. columnCountChanged ( oldCount , newCount )
参数
  • oldCount int

  • newCount int

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 )
参数
  • column int

  • oldSize int

  • newSize 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.

另请参阅

setColumnWidth()

PySide2.QtWidgets.QTreeView. columnViewportPosition ( column )
参数

column int

返回类型

int

Returns the horizontal position of the column in the viewport.

PySide2.QtWidgets.QTreeView. columnWidth ( column )
参数

column int

返回类型

int

返回宽度为 column .

PySide2.QtWidgets.QTreeView. drawBranches ( painter , rect , index )
参数
  • painter QPainter

  • rect QRect

  • index QModelIndex

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

PySide2.QtWidgets.QTreeView. drawRow ( painter , options , index )
参数

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

PySide2.QtWidgets.QTreeView. drawTree ( painter , region )
参数
  • painter QPainter

  • region QRegion

Draws the part of the tree intersecting the given region 使用指定 painter .

另请参阅

paintEvent()

PySide2.QtWidgets.QTreeView. expand ( index )
参数

index QModelIndex

Expands the model item specified by the index .

另请参阅

expanded()

PySide2.QtWidgets.QTreeView. expandAll ( )

展开所有可展开项。

警告

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

PySide2.QtWidgets.QTreeView. expandRecursively ( index [ , depth=-1 ] )
参数
  • index QModelIndex

  • depth int

Expands the item at the given index and all its children to the given depth 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 .

警告

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

另请参阅

expandAll()

PySide2.QtWidgets.QTreeView. expandToDepth ( depth )
参数

depth int

展开所有可展开项到给定 depth .

PySide2.QtWidgets.QTreeView. expanded ( index )
参数

index QModelIndex

PySide2.QtWidgets.QTreeView. expandsOnDoubleClick ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. header ( )
返回类型

QHeaderView

返回用于树视图的 Header 头。

另请参阅

setHeader() headerData()

PySide2.QtWidgets.QTreeView. hideColumn ( column )
参数

column int

隐藏 column 给定。

注意

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 ( )
返回类型

int

另请参阅

setIndentation()

PySide2.QtWidgets.QTreeView. indexAbove ( index )
参数

index QModelIndex

返回类型

QModelIndex

Returns the model index of the item above index .

PySide2.QtWidgets.QTreeView. indexBelow ( index )
参数

index QModelIndex

返回类型

QModelIndex

Returns the model index of the item below index .

PySide2.QtWidgets.QTreeView. indexRowSizeHint ( index )
参数

index QModelIndex

返回类型

int

Returns the size hint for the row indicated by index .

另请参阅

sizeHintForColumn() uniformRowHeights()

PySide2.QtWidgets.QTreeView. isAnimated ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. isColumnHidden ( column )
参数

column int

返回类型

bool

返回 true column 被隐藏;否则返回 false .

PySide2.QtWidgets.QTreeView. isExpanded ( index )
参数

index QModelIndex

返回类型

bool

返回 true 若模型项 index 被展开;否则返回 false。

PySide2.QtWidgets.QTreeView. isFirstColumnSpanned ( row , parent )
参数
  • row int

  • parent QModelIndex

返回类型

bool

返回 true 若第一列中的项在给定 row parent 跨越所有列;否则返回 false .

PySide2.QtWidgets.QTreeView. isHeaderHidden ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. isRowHidden ( row , parent )
参数
  • row int

  • parent QModelIndex

返回类型

bool

返回 true 若项在给定 row parent 被隐藏;否则返回 false .

PySide2.QtWidgets.QTreeView. isSortingEnabled ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. itemsExpandable ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. reexpand ( )
PySide2.QtWidgets.QTreeView. resetIndentation ( )
PySide2.QtWidgets.QTreeView. resizeColumnToContents ( column )
参数

column int

重置尺寸 column given to the size of its contents.

PySide2.QtWidgets.QTreeView. rootIsDecorated ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. rowHeight ( index )
参数

index QModelIndex

返回类型

int

返回指示行的高度通过给定 index .

PySide2.QtWidgets.QTreeView. rowsRemoved ( parent , first , last )
参数
  • parent QModelIndex

  • first int

  • last 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.

PySide2.QtWidgets.QTreeView. setAllColumnsShowFocus ( enable )
参数

enable bool

PySide2.QtWidgets.QTreeView. setAnimated ( enable )
参数

enable bool

另请参阅

isAnimated()

PySide2.QtWidgets.QTreeView. setAutoExpandDelay ( delay )
参数

delay int

另请参阅

autoExpandDelay()

PySide2.QtWidgets.QTreeView. setColumnHidden ( column , hide )
参数
  • column int

  • hide bool

hide 为 true column 被隐藏,否则 column 被展示。

PySide2.QtWidgets.QTreeView. setColumnWidth ( column , width )
参数
  • column int

  • width int

设置宽度为给定 column width 指定。

PySide2.QtWidgets.QTreeView. setExpanded ( index , expand )
参数
  • index QModelIndex

  • expand bool

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

PySide2.QtWidgets.QTreeView. setExpandsOnDoubleClick ( enable )
参数

enable bool

PySide2.QtWidgets.QTreeView. setFirstColumnSpanned ( row , parent , span )
参数
  • row int

  • parent QModelIndex

  • span bool

span is true the item in the first column in the row 采用给定 parent is set to span all columns, otherwise all items on the row are shown.

PySide2.QtWidgets.QTreeView. setHeader ( header )
参数

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

另请参阅

headerData()

PySide2.QtWidgets.QTreeView. setHeaderHidden ( hide )
参数

hide bool

另请参阅

isHeaderHidden()

PySide2.QtWidgets.QTreeView. setIndentation ( i )
参数

i int

另请参阅

indentation()

PySide2.QtWidgets.QTreeView. setItemsExpandable ( enable )
参数

enable bool

另请参阅

itemsExpandable()

PySide2.QtWidgets.QTreeView. setRootIsDecorated ( show )
参数

show bool

另请参阅

rootIsDecorated()

PySide2.QtWidgets.QTreeView. setRowHidden ( row , parent , hide )
参数
  • row int

  • parent QModelIndex

  • hide bool

hide 为 true row 采用给定 parent 被隐藏,否则 row 被展示。

PySide2.QtWidgets.QTreeView. setSortingEnabled ( enable )
参数

enable bool

PySide2.QtWidgets.QTreeView. setTreePosition ( logicalIndex )
参数

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.

PySide2.QtWidgets.QTreeView. setUniformRowHeights ( uniform )
参数

uniform bool

PySide2.QtWidgets.QTreeView. setWordWrap ( on )
参数

on bool

另请参阅

wordWrap()

PySide2.QtWidgets.QTreeView. showColumn ( column )
参数

column int

展示给定 column 在树视图。

PySide2.QtWidgets.QTreeView. sortByColumn ( column , order )
参数
  • column int

  • order SortOrder

Sorts the model 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.

另请参阅

sortingEnabled

PySide2.QtWidgets.QTreeView. sortByColumn ( column )
参数

column int

注意

此函数被弃用。

这是重载函数。

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

PySide2.QtWidgets.QTreeView. treePosition ( )
返回类型

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.

另请参阅

setTreePosition()

PySide2.QtWidgets.QTreeView. uniformRowHeights ( )
返回类型

bool

PySide2.QtWidgets.QTreeView. wordWrap ( )
返回类型

bool

另请参阅

setWordWrap()