QTreeView Class Reference

[ QtGui module]

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

继承 QAbstractItemView .

Inherited by QHelpContentWidget and QTreeWidget .

方法

Qt Signals


详细描述

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.

QTreeView 类是一种 模型/视图类 且属于 Qt 的 模型/视图框架 .

QTreeView 实现的接口定义通过 QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel 类。

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 并作为树显示:

     QFileSystemModel *model = new QFileSystemModel;
     model->setRootPath(QDir.currentPath());
     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 () 或 expanded () 信号被发射 with the model index of the relevant item.

The amount of indentation used to indicate levels of hierarchy is controlled by the indentation 特性。

树视图 Header 头部的构造使用 QHeaderView 类且可以被隐藏使用 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.

键绑定

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
向上 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.
星号 Expands all children of the current item (if present).
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.
Screenshot of a Windows XP style tree view Screenshot of a Macintosh style tree view Screenshot of a Plastique style tree view
A Windows XP style tree view. A Macintosh style tree view. A Plastique style tree view.

改善性能

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.


方法文档编制

QTreeView.__init__ ( self , QWidget   parent  = None)

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

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

另请参阅 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&) .

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

另请参阅 collapsed ().

QTreeView.collapseAll ( self )

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

折叠所有展开项。

该函数在 Qt 4.2 引入。

另请参阅 expandAll (), expand (), collapse (),和 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.

另请参阅 setColumnWidth ().

int QTreeView.columnViewportPosition ( self , int  column )

Returns the horizontal position of the column 在 viewport.

int QTreeView.columnWidth ( self , int  column )

返回宽度为 column .

另请参阅 resizeColumnToContents () and setColumnWidth ().

QTreeView.currentChanged ( self , QModelIndex   current , QModelIndex   上一 )

重实现自 QAbstractItemView.currentChanged ().

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

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

重实现自 QAbstractItemView.dataChanged ().

QTreeView.dragMoveEvent ( self , QDragMoveEvent   event )

重实现自 QWidget.dragMoveEvent ().

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 ,使用 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 ,使用 painter given. The option control how the item is displayed.

另请参阅 setAlternatingRowColors ().

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

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

该函数在 Qt 4.2 引入。

另请参阅 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 .

另请参阅 expanded ().

QTreeView.expandAll ( self )

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

展开所有可展开项。

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

该函数在 Qt 4.2 引入。

另请参阅 collapseAll (), expand (), collapse (),和 setExpanded ().

bool QTreeView.expandsOnDoubleClick ( self )

QTreeView.expandToDepth ( self , int  depth )

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

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

该函数在 Qt 4.3 引入。

另请参阅 expandAll (), collapseAll (), expand (), collapse (),和 setExpanded ().

QHeaderView QTreeView.header ( self )

返回用于树视图的 Header 头。

另请参阅 setHeader () 和 QAbstractItemModel.headerData ().

QTreeView.hideColumn ( self , int  column )

This method is also a Qt slot with the C++ signature void hideColumn(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 .

另请参阅 showColumn () 和 setColumnHidden ().

int QTreeView.horizontalOffset ( self )

重实现自 QAbstractItemView.horizontalOffset ().

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.

另请参阅 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 )

重实现自 QAbstractItemView.indexAt ().

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 .

另请参阅 sizeHintForColumn () 和 uniformRowHeights ().

bool QTreeView.isAnimated ( self )

bool QTreeView.isColumnHidden ( self , int  column )

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

另请参阅 hideColumn () 和 isRowHidden ().

bool QTreeView.isExpanded ( self , QModelIndex   index )

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

另请参阅 expand (), expanded (),和 setExpanded ().

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

Returns true if the item in first column in the given row parent 跨越所有列;否则返回 false.

该函数在 Qt 4.3 引入。

另请参阅 setFirstColumnSpanned ().

bool QTreeView.isHeaderHidden ( self )

bool QTreeView.isIndexHidden ( self , QModelIndex   index )

重实现自 QAbstractItemView.isIndexHidden ().

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

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

另请参阅 setRowHidden () 和 isColumnHidden ().

bool QTreeView.isSortingEnabled ( self )

bool QTreeView.itemsExpandable ( self )

QTreeView.keyboardSearch ( self , QString  search )

重实现自 QAbstractItemView.keyboardSearch ().

QTreeView.keyPressEvent ( self , QKeyEvent   event )

重实现自 QWidget.keyPressEvent ().

QTreeView.mouseDoubleClickEvent ( self , QMouseEvent   e )

重实现自 QWidget.mouseDoubleClickEvent ().

QTreeView.mouseMoveEvent ( self , QMouseEvent   event )

重实现自 QWidget.mouseMoveEvent ().

QTreeView.mousePressEvent ( self , QMouseEvent   e )

重实现自 QWidget.mousePressEvent ().

QTreeView.mouseReleaseEvent ( self , QMouseEvent   event )

重实现自 QWidget.mouseReleaseEvent ().

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

重实现自 QAbstractItemView.moveCursor ().

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

QTreeView.paintEvent ( self , QPaintEvent   e )

重实现自 QWidget.paintEvent ().

QTreeView.reexpand ( self )

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

QTreeView.reset ( self )

重实现自 QAbstractItemView.reset ().

QTreeView.resizeColumnToContents ( self , int  column )

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

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

另请参阅 columnWidth () 和 setColumnWidth ().

bool QTreeView.rootIsDecorated ( self )

int QTreeView.rowHeight ( self , QModelIndex   index )

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

该函数在 Qt 4.3 引入。

另请参阅 indexRowSizeHint ().

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

重实现自 QAbstractItemView.rowsAboutToBeRemoved ().

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.

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

重实现自 QAbstractItemView.rowsInserted ().

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

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.

该函数在 Qt 4.1 引入。

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

重实现自 QAbstractScrollArea.scrollContentsBy ().

滚动树视图内容通过 ( dx , dy ).

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

重实现自 QAbstractItemView.scrollTo ().

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.

QTreeView.selectAll ( self )

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

重实现自 QAbstractItemView.selectAll ().

list-of-QModelIndex QTreeView.selectedIndexes ( self )

重实现自 QAbstractItemView.selectedIndexes ().

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

重实现自 QAbstractItemView.selectionChanged ().

QTreeView.setAllColumnsShowFocus ( self , bool  enable )

QTreeView.setAnimated ( self , bool  enable )

QTreeView.setAutoExpandDelay ( self , int  delay )

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

hide 为 true column is hidden, otherwise the column 被展示。

另请参阅 isColumnHidden (), hideColumn (),和 setRowHidden ().

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

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

该函数在 Qt 4.2 引入。

另请参阅 columnWidth () 和 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 .

另请参阅 expanded (), expand (),和 isExpanded ().

QTreeView.setExpandsOnDoubleClick ( self , bool  enable )

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

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.

该函数在 Qt 4.3 引入。

另请参阅 isFirstColumnSpanned ().

QTreeView.setHeader ( self , QHeaderView   header )

header argument has it's ownership transferred to Qt.

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.

另请参阅 QAbstractItemModel.headerData ().

QTreeView.setHeaderHidden ( self , bool  hide )

QTreeView.setIndentation ( self , int  i )

QTreeView.setItemsExpandable ( self , bool  enable )

QTreeView.setModel ( self , QAbstractItemModel   model )

重实现自 QAbstractItemView.setModel ().

QTreeView.setRootIndex ( self , QModelIndex   index )

重实现自 QAbstractItemView.setRootIndex ().

QTreeView.setRootIsDecorated ( self , bool  show )

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

hide 为 true row with the given parent 被隐藏,否则 row 被展示。

另请参阅 isRowHidden () 和 setColumnHidden ().

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

重实现自 QAbstractItemView.setSelection ().

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

另请参阅 selectionCommand ().

QTreeView.setSelectionModel ( self , QItemSelectionModel   selectionModel )

重实现自 QAbstractItemView.setSelectionModel ().

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

展示给定 column 在树视图。

另请参阅 hideColumn () 和 setColumnHidden ().

int QTreeView.sizeHintForColumn ( self , int  column )

重实现自 QAbstractItemView.sizeHintForColumn ().

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.

另请参阅 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 .

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.

该函数在 Qt 4.2 引入。

另请参阅 sortingEnabled .

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

QTreeView.timerEvent ( self , QTimerEvent   event )

重实现自 QObject.timerEvent ().

bool QTreeView.uniformRowHeights ( self )

QTreeView.updateGeometries ( self )

重实现自 QAbstractItemView.updateGeometries ().

int QTreeView.verticalOffset ( self )

重实现自 QAbstractItemView.verticalOffset ().

返回树视图中项的垂直偏移。

另请参阅 horizontalOffset ().

bool QTreeView.viewportEvent ( self , QEvent   event )

重实现自 QAbstractScrollArea.viewportEvent ().

QRect QTreeView.visualRect ( self , QModelIndex   index )

重实现自 QAbstractItemView.visualRect ().

返回项所占据的视口矩形在 index . If the index is not visible or explicitly hidden, the returned rectangle is invalid.

QRegion QTreeView.visualRegionForSelection ( self , QItemSelection   selection )

重实现自 QAbstractItemView.visualRegionForSelection ().

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

Since 4.7, the returned region only contains rectangles intersecting (or included in) the viewport.

bool QTreeView.wordWrap ( self )


Qt Signal Documentation

void collapsed (const QModelIndex&)

This is the default overload of this signal.

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

void expanded (const QModelIndex&)

This is the default overload of this signal.

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

另请参阅 setExpanded ().