QTreeView Class
QTreeView 类提供了一种树视图的默认模型/视图实现。更多内容...
| 头文件: | #include <QTreeView> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
| qmake: | QT += widgets |
| 继承自: | QAbstractItemView |
| 继承自: |
属性
|
|
公共函数
| QTreeView(QWidget *parent = nullptr) | |
| virtual | ~QTreeView() |
| bool | allColumnsShowFocus() const |
| int | autoExpandDelay() const |
| int | columnAt(int x) const |
| int | columnViewportPosition(int column) const |
| int | columnWidth(int column) const |
| bool | expandsOnDoubleClick() const |
| QHeaderView * | header() const |
| int | indentation() const |
| QModelIndex | indexAbove(const QModelIndex &index) const |
| QModelIndex | indexBelow(const QModelIndex &index) const |
| bool | isAnimated() const |
| bool | isColumnHidden(int column) const |
| bool | isExpanded(const QModelIndex &index) const |
| bool | isFirstColumnSpanned(int row, const QModelIndex &parent) const |
| bool | isHeaderHidden() const |
| bool | isRowHidden(int row, const QModelIndex &parent) const |
| bool | isSortingEnabled() const |
(since 6.12) QRect | itemDecorationRect(const QModelIndex &index) const |
| bool | itemsExpandable() const |
| void | resetIndentation() |
| bool | rootIsDecorated() const |
| void | setAllColumnsShowFocus(bool enable) |
| void | setAnimated(bool enable) |
| void | setAutoExpandDelay(int delay) |
| void | setColumnHidden(int column, bool hide) |
| void | setColumnWidth(int column, int width) |
| void | setExpanded(const QModelIndex &index, bool expanded) |
| void | setExpandsOnDoubleClick(bool enable) |
| void | setFirstColumnSpanned(int row, const QModelIndex &parent, bool span) |
| void | setHeader(QHeaderView *header) |
| void | setHeaderHidden(bool hide) |
| void | setIndentation(int i) |
| void | setItemsExpandable(bool enable) |
| void | setRootIsDecorated(bool show) |
| void | setRowHidden(int row, const QModelIndex &parent, bool hide) |
| void | setSortingEnabled(bool enable) |
| void | setTreePosition(int index) |
| void | setUniformRowHeights(bool uniform) |
| void | setWordWrap(bool on) |
| int | treePosition() const |
| bool | uniformRowHeights() const |
| bool | wordWrap() const |
重新实现的公共函数
| virtual void | dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles = QList<int>()) override |
| virtual QModelIndex | indexAt(const QPoint &point) const override |
| virtual void | keyboardSearch(const QString &search) override |
| virtual void | reset() override |
| virtual void | scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint = EnsureVisible) override |
| virtual void | selectAll() override |
| virtual void | setModel(QAbstractItemModel *model) override |
| virtual void | setRootIndex(const QModelIndex &index) override |
| virtual void | setSelectionModel(QItemSelectionModel *selectionModel) override |
| virtual QRect | visualRect(const QModelIndex &index) const override |
公共插槽
| void | collapse(const QModelIndex &index) |
| void | collapseAll() |
| void | expand(const QModelIndex &index) |
| void | expandAll() |
| void | expandRecursively(const QModelIndex &index, int depth = -1) |
| void | expandToDepth(int depth) |
| void | hideColumn(int column) |
| void | resizeColumnToContents(int column) |
| void | showColumn(int column) |
| void | sortByColumn(int column, Qt::SortOrder order) |
信号
受保护函数
| virtual void | drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const |
| virtual void | drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| void | drawTree(QPainter *painter, const QRegion ®ion) const |
| int | indexRowSizeHint(const QModelIndex &index) const |
| int | rowHeight(const QModelIndex &index) const |
重新实现的受保护函数
| virtual void | changeEvent(QEvent *event) override |
| virtual void | currentChanged(const QModelIndex ¤t, const QModelIndex &previous) override |
| virtual void | dragMoveEvent(QDragMoveEvent *event) override |
| virtual int | horizontalOffset() const override |
| virtual bool | isIndexHidden(const QModelIndex &index) const override |
| virtual void | keyPressEvent(QKeyEvent *event) override |
| virtual void | mouseDoubleClickEvent(QMouseEvent *event) override |
| virtual void | mouseMoveEvent(QMouseEvent *event) override |
| virtual void | mousePressEvent(QMouseEvent *event) override |
| virtual void | mouseReleaseEvent(QMouseEvent *event) override |
| virtual QModelIndex | moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override |
| virtual void | paintEvent(QPaintEvent *event) override |
| virtual void | rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override |
| virtual void | rowsInserted(const QModelIndex &parent, int start, int end) override |
| virtual void | scrollContentsBy(int dx, int dy) override |
| virtual QModelIndexList | selectedIndexes() const override |
| virtual void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override |
| virtual void | setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) override |
| virtual int | sizeHintForColumn(int column) const override |
| virtual void | timerEvent(QTimerEvent *event) override |
| virtual void | updateGeometries() override |
| virtual int | verticalOffset() const override |
| virtual bool | viewportEvent(QEvent *event) override |
| virtual QSize | viewportSizeHint() const override |
| virtual QRegion | visualRegionForSelection(const QItemSelection &selection) const override |
受保护的槽
| void | columnCountChanged(int oldCount, int newCount) |
| void | columnMoved() |
| void | columnResized(int column, int oldSize, int newSize) |
| void | rowsRemoved(const QModelIndex &parent, int start, int end) |
详细说明

QTreeView 实现了模型中项目的树形表示。该类用于提供以前由QListView 类提供的标准层次列表,但采用了 Qt 模型/视图架构提供的更灵活的方法。
QTreeView 类是模型/视图类之一,属于 Qt的模型/视图框架。
QTreeView 实现了由QAbstractItemView 类定义的接口,从而能够显示由继承自QAbstractItemModel 类的模型提供的数据。
构建一个显示模型数据的树视图非常简单。在下面的示例中,目录的内容由一个 `QFileSystemModel ` 提供,并以树的形式显示:
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
QTreeView *tree = new QTreeView(splitter);
tree->setModel(model);模型/视图架构确保随着模型的变化,树视图的内容也会随之更新。
具有子节点的项目可以处于展开状态(子节点可见)或折叠状态(子节点隐藏)。当该状态发生变化时,会发出一个collapsed()或expanded()信号,并携带相关项目的模型索引。
用于表示层次结构级别的缩进程度由indentation 属性控制。
树视图中的标题行使用 `QHeaderView ` 类构建,并可通过 `header()->hide()` 进行隐藏。请注意,每个标题行的 `stretchLastSection ` 属性均配置为 `true`,以确保视图不会浪费分配给标题行的任何空间。如果该值设置为 `true`,则此属性将覆盖标题行最后一个部分上设置的调整大小模式。
默认情况下,树视图中除第一个列外,所有列均可移动。若要禁用这些列的移动,请使用QHeaderView 的setSectionsMovable()函数。有关重新排列部分的更多信息,请参阅Moving Header Sections 。
键盘快捷键
QTreeView 支持一组键盘快捷键,允许用户在视图中导航并与项目内容进行交互:
| 键 | 操作 |
|---|---|
| 上 | 将光标移动到上一行同一列中的项目。如果当前项目的父项已无更多行可导航,则光标将移动到该父项之前那个兄弟项的最后一行中的相应项目。 |
| 向下 | 将光标移至同一列中下一行中的项目。如果当前项目的父项已无行可导航,则光标将移至该父项之前兄弟项的第一行中的相应项目。 |
| 左 | 通过折叠分支来隐藏当前项目的子项(如有)。 |
| 减号 | 与“左”功能相同。 |
| 右 | 通过展开分支来显示当前项目的子项(如有)。 |
| 加号 | 与“右”相同。 |
| 星号 | 展开当前项目及其所有子项(如有)。 |
| PageUp | 将光标向上移动一页。 |
| PageDown将光标向下移动一页。 | 将光标向下移动一页。 |
| Home | 将光标移至模型中第一个顶级项目的首行同一列中的某个项目上。 |
| End | 将光标移至模型中最后一个顶级项目最后一行的同一列中的某个项目。 |
| F2 | 在可编辑模型中,此操作将打开当前项目以供编辑。可使用“Esc”键取消编辑过程,并将显示的数据恢复为原始状态。 |
提高性能
为了在显示大量项目时提高性能,可以向视图提供有关其所处理数据的提示。对于旨在显示高度相等项目的视图,一种可采用的方法是将uniformRowHeights 属性设置为true。
另请参阅 QListView 、QTreeWidget 、View Classes、QAbstractItemModel 以及QAbstractItemView 。
属性文档
allColumnsShowFocus : bool
该属性控制项目是否应通过所有列显示键盘焦点
如果此属性设置为true ,则所有列都会显示焦点;否则,仅有一列显示焦点。
默认值为 false。
访问函数:
| bool | allColumnsShowFocus() const |
| void | setAllColumnsShowFocus(bool enable) |
animated : bool
该属性用于控制是否启用动画
如果该属性设置为true ,树视图将对分支的展开和折叠进行动画效果。如果该属性设置为false ,树视图将立即展开或折叠分支,而不显示动画效果。
默认情况下,该属性值为false 。
访问函数:
| bool | isAnimated() const |
| void | setAnimated(bool enable) |
autoExpandDelay : int
此属性用于指定在拖放操作中,树中的项目被打开前的延迟时间。
该属性指定用户将光标悬停在某个节点上后,该节点自动展开所需的延迟时间(以毫秒为单位)。如果该时间设置为小于 0,则该功能将不会被激活。
默认情况下,该属性的值为 -1,表示自动展开功能已禁用。
访问函数:
| int | autoExpandDelay() const |
| void | setAutoExpandDelay(int delay) |
expandsOnDoubleClick : bool
该属性用于指定是否可以通过双击展开项目。
该属性用于指定用户是否可以通过双击来展开和折叠项目。默认值为 true。
访问函数:
| bool | expandsOnDoubleClick() const |
| void | setExpandsOnDoubleClick(bool enable) |
另请参阅 itemsExpandable 。
headerHidden : bool
该属性用于控制标题是否显示。
如果该属性值为true ,则不显示页眉;否则显示页眉。默认值为false。
访问函数:
| bool | isHeaderHidden() const |
| void | setHeaderHidden(bool hide) |
另请参阅 header()。
indentation : int
树视图中项目的缩进。
该属性存储树视图中各层级项目以像素为单位的缩进值。对于顶级项目,该缩进值指定从视口边缘到第一列项目的水平距离;对于子项目,它指定其相对于父项目的缩进距离。
默认情况下,该属性的值取决于样式。因此,当样式发生变化时,该属性会据此更新。调用 setIndentation() 会停止更新,调用 resetIndentation() 将恢复默认行为。
访问函数:
| int | indentation() const |
| void | setIndentation(int i) |
| void | resetIndentation() |
itemsExpandable : bool
该属性用于指定用户是否可以展开这些项目。
该属性表示用户是否可以交互式地展开和折叠项目。
默认情况下,此属性的值为true 。
访问函数:
| bool | itemsExpandable() const |
| void | setItemsExpandable(bool enable) |
rootIsDecorated : bool
该属性控制是否显示用于展开和折叠顶级项目的控件
通常,带有子节点的项目会显示展开和折叠控件,以便显示或隐藏其子节点。如果此属性为 false,则顶级项目不会显示这些控件。这可用于使单级树结构看起来像一个简单的项目列表。
默认情况下,此属性为true 。
访问函数:
| bool | rootIsDecorated() const |
| void | setRootIsDecorated(bool show) |
sortingEnabled : bool
该属性用于指定是否启用排序
如果该属性值为true ,则树的排序功能已启用;如果该属性值为false,则排序功能未启用。默认值为false。
注意:为 避免性能问题,建议在将项目插入树后再启用排序。或者,您也可以先将项目插入列表,然后再将其插入树中。
访问函数:
| bool | isSortingEnabled() const |
| void | setSortingEnabled(bool enable) |
另请参阅 sortByColumn()。
uniformRowHeights : bool
该属性表示树视图中的所有项目是否具有相同的高度
只有在能保证视图中的所有项目高度完全相同时,才应将此属性设置为 true。这将使视图能够进行一些优化。
高度值取自视图中的第一个项目。当该项目的数据发生变化时,该值会随之更新。
注意:如果 编辑器的大小提示值大于单元格的大小提示值,则将使用编辑器的大小提示值。
默认情况下,该属性值为false 。
访问函数:
| bool | uniformRowHeights() const |
| void | setUniformRowHeights(bool uniform) |
wordWrap : bool
此属性用于指定项目文本的换行策略
如果此属性的值是true ,则项目文本会在必要时按单词分隔点进行换行;否则,文本将完全不换行。此属性的默认值为false 。
请注意,即使启用了换行,单元格也不会扩展以容纳所有文本。将根据当前的textElideMode 插入省略号。
Access 函数:
| bool | wordWrap() const |
| void | setWordWrap(bool on) |
成员函数文档
[explicit] QTreeView::QTreeView(QWidget *parent = nullptr)
构建一个带有parent 的树形视图,用于展示模型的数据。使用setModel()来设置模型。
另请参阅 QAbstractItemModel 。
[virtual noexcept] QTreeView::~QTreeView()
销毁树形视图。
[override virtual protected] void QTreeView::changeEvent(QEvent *event)
重写了:QFrame::changeEvent(QEvent *ev)。
[slot] void QTreeView::collapse(const QModelIndex &index)
将由index 指定的模型项折叠。
另请参阅 collapsed()。
[slot] void QTreeView::collapseAll()
折叠所有已展开的项目。
另请参阅 expandAll()、expand()、collapse() 以及setExpanded()。
[signal] void QTreeView::collapsed(const QModelIndex &index)
当由 `index ` 指定的项目被折叠时,会触发此信号。
int QTreeView::columnAt(int x) const
返回树视图中标题覆盖给定坐标(x )的列。
[protected slot] void QTreeView::columnCountChanged(int oldCount, int newCount)
通知树视图,其列数已从oldCount 更改为newCount 。
[protected slot] void QTreeView::columnMoved()
每当移动了一列时,都会调用此回调函数。
[protected slot] void QTreeView::columnResized(int column, int oldSize, int newSize)
每当头文件中column 的大小发生变化时,都会调用此函数。oldSize 和newSize 分别返回以前的大小和新大小(单位为像素)。
另请参阅 setColumnWidth()。
int QTreeView::columnViewportPosition(int column) const
返回column 在视口中的水平位置。
int QTreeView::columnWidth(int column) const
返回column 的宽度。
另请参阅 resizeColumnToContents() 和setColumnWidth()。
[override virtual protected] void QTreeView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
重写了:QAbstractItemView::currentChanged (const QModelIndex ¤t, const QModelIndex &previous)。
[override virtual] void QTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles = QList<int>())
重写了:QAbstractItemView::dataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles)。
[override virtual protected] void QTreeView::dragMoveEvent(QDragMoveEvent *event)
重写了:QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)。
[virtual protected] void QTreeView::drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const
使用给定的painter ,将树视图中的分支绘制在与模型项index 同一行上。这些分支将绘制在由rect 指定的矩形内。
[virtual protected] void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
使用给定的painter ,在树视图中绘制包含模型项index 的行。option 控制该项的显示方式。
另请参阅 setAlternatingRowColors()。
[protected] void QTreeView::drawTree(QPainter *painter, const QRegion ®ion) const
使用指定的painter ,绘制与给定的region 相交的树的一部分。
另请参阅 paintEvent()。
[slot] void QTreeView::expand(const QModelIndex &index)
展开由index 指定的模型项。
另请参阅 expanded()。
[slot] void QTreeView::expandAll()
展开所有可展开的项目。
注意:此 函数不会尝试对数据进行fetch more 处理。
警告:如果 模型包含大量项目,此函数的执行将需要一些时间。
另请参阅 collapseAll()、expand()、collapse() 和setExpanded()。
[slot] void QTreeView::expandRecursively(const QModelIndex &index, int depth = -1)
将位于指定位置(index )的项目及其所有子项展开至指定位置(depth )。展开位置(depth )相对于指定位置(index )。若展开位置(depth )为-1,则展开所有子项;若展开位置(depth )为0,则仅展开指定位置(index )处的项目。
注意:此 函数不会尝试fetch more 数据。
警告:如果 模型包含大量项目,此函数的执行将需要一些时间。
另请参阅 expandAll()。
[slot] void QTreeView::expandToDepth(int depth)
将所有可展开的项目展开至指定的depth 。
注意:此 函数不会尝试对数据进行fetch more 处理。
另请参阅 expandAll()、collapseAll()、expand()、collapse(),以及setExpanded()。
[signal] void QTreeView::expanded(const QModelIndex &index)
当由 `index ` 指定的项目被展开时,会发出此信号。
另请参阅 setExpanded()。
QHeaderView *QTreeView::header() const
返回树视图的标题。
另请参阅 setHeader() 和QAbstractItemModel::headerData()。
[slot] void QTreeView::hideColumn(int column)
隐藏指定的column 。
注意:此 函数应仅在模型初始化完成后调用,因为视图需要知道列的数量才能隐藏column 。
另请参阅 showColumn() 和setColumnHidden()。
[override virtual protected] int QTreeView::horizontalOffset() const
重写:QAbstractItemView::horizontalOffset() const。
返回树视图中项目的水平偏移量。
请注意,树视图使用水平标题栏的位置来确定视图中各列的位置。
另请参阅 verticalOffset()。
QModelIndex QTreeView::indexAbove(const QModelIndex &index) const
返回位于index 上方该项的模型索引。
[override virtual] QModelIndex QTreeView::indexAt(const QPoint &point) const
重写了:QAbstractItemView::indexAt(const QPoint &point) const。
QModelIndex QTreeView::indexBelow(const QModelIndex &index) const
返回index 下该项的模型索引。
[protected] int QTreeView::indexRowSizeHint(const QModelIndex &index) const
返回由 `index` 指定的行的大小提示。
另请参阅 sizeHintForColumn() 和uniformRowHeights()。
bool QTreeView::isColumnHidden(int column) const
如果column 被隐藏,则返回true ;否则返回false 。
另请参阅 hideColumn() 和isRowHidden()。
bool QTreeView::isExpanded(const QModelIndex &index) const
如果模型项index 已展开,则返回true ;否则返回 false。
另请参阅 expand()、expanded() 和setExpanded()。
bool QTreeView::isFirstColumnSpanned(int row, const QModelIndex &parent) const
如果给定的row 中parent 的第一列中的项横跨所有列,则返回true ;否则返回false 。
另请参阅 setFirstColumnSpanned()。
[override virtual protected] bool QTreeView::isIndexHidden(const QModelIndex &index) const
重写了:QAbstractItemView::isIndexHidden(const QModelIndex &index) const。
bool QTreeView::isRowHidden(int row, const QModelIndex &parent) const
如果parent 中给定的row 项被隐藏,则返回true ;否则返回false 。
另请参阅 setRowHidden() 和isColumnHidden()。
[since 6.12] QRect QTreeView::itemDecorationRect(const QModelIndex &index) const
返回位于index 处的项的展开/折叠指示器所占用的矩形。如果该项没有此类指示器(例如因为没有子项,或者因为禁用了根装饰),则返回的矩形无效。无效的index 也会返回一个无效的矩形。
该函数在 Qt 6.12 中引入。
另请参阅 visualRect() 和QStyle::SE_TreeViewDisclosureItem 。
[override virtual protected] void QTreeView::keyPressEvent(QKeyEvent *event)
重写了:QAbstractItemView::keyPressEvent(QKeyEvent *event)。
[override virtual] void QTreeView::keyboardSearch(const QString &search)
重写了:QAbstractItemView::keyboardSearch(const QString &search)。
[override virtual protected] void QTreeView::mouseDoubleClickEvent(QMouseEvent *event)
重写了:QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)。
[override virtual protected] void QTreeView::mouseMoveEvent(QMouseEvent *event)
重写了:QAbstractItemView::mouseMoveEvent(QMouseEvent *event)。
[override virtual protected] void QTreeView::mousePressEvent(QMouseEvent *event)
重写了:QAbstractItemView::mousePressEvent(QMouseEvent *event)。
[override virtual protected] void QTreeView::mouseReleaseEvent(QMouseEvent *event)
重写了:QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)。
[override virtual protected] QModelIndex QTreeView::moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
重写:QAbstractItemView::moveCursor (QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers)。
根据cursorAction 中描述的方式移动光标,并使用按钮modifiers 提供的信息。
[override virtual protected] void QTreeView::paintEvent(QPaintEvent *event)
重写了:QAbstractScrollArea::paintEvent(QPaintEvent *event)。
[override virtual] void QTreeView::reset()
重写了:QAbstractItemView::reset()。
[slot] void QTreeView::resizeColumnToContents(int column)
将传入的column 调整为与其内容大小相符的尺寸。
另请参阅 columnWidth()、setColumnWidth()、sizeHintForColumn() 以及QHeaderView::resizeContentsPrecision()。
[protected] int QTreeView::rowHeight(const QModelIndex &index) const
返回由给定的index 指定的行的高度。
另请参阅 indexRowSizeHint()。
[override virtual protected] void QTreeView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
重写:QAbstractItemView::rowsAboutToBeRemoved (const QModelIndex &parent, int start, int end)。
通知视图,从start 行到end 行(含)的行即将从给定的parent 模型项中移除。
[override virtual protected] void QTreeView::rowsInserted(const QModelIndex &parent, int start, int end)
重写:QAbstractItemView::rowsInserted (const QModelIndex &parent, int start, int end)。
通知视图,从start 行到end 行(含)的行已插入到parent 模型项中。
[protected slot] void QTreeView::rowsRemoved(const QModelIndex &parent, int start, int end)
告知视图,从start 行到end 行(含)的行已从给定的parent 模型项中删除。
[override virtual protected] void QTreeView::scrollContentsBy(int dx, int dy)
重写:QAbstractScrollArea::scrollContentsBy(int dx, int dy)。
将树视图的内容按 (dx,dy) 滚动。
[override virtual] void QTreeView::scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint = EnsureVisible)
重写自:QAbstractItemView::scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint)。
滚动树视图的内容,直到给定的模型项index 可见。hint 参数更精确地指定了该项在操作完成后应位于的位置。如果该模型项的任何父项处于折叠状态,则会将其展开,以确保该模型项可见。
[override virtual] void QTreeView::selectAll()
重写了:QAbstractItemView::selectAll()。
[override virtual protected] QModelIndexList QTreeView::selectedIndexes() const
重新实现了:QAbstractItemView::selectedIndexes() const。
[override virtual protected] void QTreeView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
重写了:QAbstractItemView::selectionChanged (const QItemSelection &selected, const QItemSelection &deselected)。
void QTreeView::setColumnHidden(int column, bool hide)
如果hide 为真,则column 被隐藏;否则,column 将显示出来。
另请参阅 isColumnHidden()、hideColumn() 和setRowHidden()。
void QTreeView::setColumnWidth(int column, int width)
将给定的column 的宽度设置为指定的width 。
另请参阅 columnWidth() 和resizeColumnToContents()。
void QTreeView::setExpanded(const QModelIndex &index, bool expanded)
根据expanded 的值,将index 所引用的项设置为折叠或展开状态。
另请参阅 expanded()、expand() 和isExpanded()。
void QTreeView::setFirstColumnSpanned(int row, const QModelIndex &parent, bool span)
如果span 为真,则在row 中,具有给定parent 的第一列项目将被设置为横跨所有列;否则,将显示row 上的所有项目。
另请参阅 isFirstColumnSpanned()。
void QTreeView::setHeader(QHeaderView *header)
将树视图的标题设置为给定的header 。
该视图将接管指定的header ,并在设置新标题时将其删除。
另请参阅 QAbstractItemModel::headerData()。
[override virtual] void QTreeView::setModel(QAbstractItemModel *model)
重写了:QAbstractItemView::setModel(QAbstractItemModel *model)。
[override virtual] void QTreeView::setRootIndex(const QModelIndex &index)
重写了:QAbstractItemView::setRootIndex(const QModelIndex &index)。
void QTreeView::setRowHidden(int row, const QModelIndex &parent, bool hide)
如果hide 为 true,则带有指定parent 的row 会被隐藏;否则,row 将被显示。
另请参阅 isRowHidden() 和setColumnHidden()。
[override virtual protected] void QTreeView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
重写了:QAbstractItemView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)。
将选择操作command 应用于矩形内的项目或被该矩形触及的项目,rect 。
另请参阅 selectionCommand()。
[override virtual] void QTreeView::setSelectionModel(QItemSelectionModel *selectionModel)
重写:QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)。
void QTreeView::setTreePosition(int index)
这指定树结构应位于逻辑索引index 处。如果将 设置为 -1,则该树将始终位于视觉索引 0 处。
另请参阅 treePosition()、QHeaderView::swapSections() 和QHeaderView::moveSection()。
[slot] void QTreeView::showColumn(int column)
在树形视图中显示给定的column 。
另请参阅 hideColumn() 和setColumnHidden()。
[override virtual protected] int QTreeView::sizeHintForColumn(int column) const
重写:QAbstractItemView::sizeHintForColumn(int column) const。
返回column 的宽度大小提示,如果没有模型则返回 -1。
如果需要将给定列的宽度设置为固定值,请在视图的标题栏上调用QHeaderView::resizeSection()。
若在子类中重写此函数,请注意:您返回的值仅在调用 `resizeColumnToContents()` 时才会被使用。在这种情况下,如果视图的标题栏或项目委托方需要更大的列宽,则会优先使用该宽度。
另请参阅 QWidget::sizeHint 、header() 和QHeaderView::resizeContentsPrecision()。
[slot] void QTreeView::sortByColumn(int column, Qt::SortOrder order)
根据给定的column 和order 中的值对模型进行排序。
column 该值可以是 -1,此时不会显示任何排序标记,模型将恢复到其自然、未排序的顺序。请注意,并非所有模型都支持此功能,甚至可能因此崩溃。
另请参阅 sortingEnabled 。
[override virtual protected] void QTreeView::timerEvent(QTimerEvent *event)
重写了:QAbstractItemView::timerEvent(QTimerEvent *event)。
int QTreeView::treePosition() const
返回树所设置的逻辑索引。如果返回值为 -1,则表示树位于视觉索引 0 处。
另请参阅 setTreePosition()。
[override virtual protected] void QTreeView::updateGeometries()
重写了:QAbstractItemView::updateGeometries()。
[override virtual protected] int QTreeView::verticalOffset() const
重写:QAbstractItemView::verticalOffset() const。
返回树视图中项的垂直偏移量。
另请参阅 horizontalOffset()。
[override virtual protected] bool QTreeView::viewportEvent(QEvent *event)
重写了:QAbstractItemView::viewportEvent(QEvent *event)。
[override virtual protected] QSize QTreeView::viewportSizeHint() const
重新实现了:QAbstractItemView::viewportSizeHint() const。
[override virtual] QRect QTreeView::visualRect(const QModelIndex &index) const
重写:QAbstractItemView::visualRect(const QModelIndex &index) const。
返回在index 处该项在视口内所占的矩形区域。如果该索引不可见或被显式隐藏,则返回的矩形区域无效。
另请参阅 itemDecorationRect()。
[override virtual protected] QRegion QTreeView::visualRegionForSelection(const QItemSelection &selection) const
重写:QAbstractItemView::visualRegionForSelection(const QItemSelection &selection) const。
返回给定selection 中各项在视口内的矩形区域。
自 4.7 版本起,返回的区域仅包含与视口相交(或包含在视口内)的矩形。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.