QTreeView Class

QTreeView 类提供了树形视图的默认模型/视图实现。更多

Header: #include <QTreeView>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QAbstractItemView
继承于

QHelpContentWidgetQTreeWidget

属性

公共函数

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

信号

void collapsed(const QModelIndex &index)
void expanded(const QModelIndex &index)

受保护函数

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 &region) const
int indexRowSizeHint(const QModelIndex &index) const
int rowHeight(const QModelIndex &index) const

重新实现的受保护函数

virtual void changeEvent(QEvent *event) override
virtual void currentChanged(const QModelIndex &current, 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,该属性将覆盖页眉中最后一个部分所设置的调整大小模式。

默认情况下,除第一列外,树形视图中的所有列都是可移动的。要禁用这些列的移动,请使用QHeaderViewsetSectionsMovable() 函数。有关重新排列部分的更多信息,请参阅Moving Header Sections

键绑定

QTreeView 支持一组键绑定,使用户可以在视图中导航并与项目内容交互:

操作
向上将光标移动到上一行同一列中的项目。如果当前项的父项没有更多的行可供导航,则光标会移动到父项前面的同级项最后一行中的相关项。
向下将光标移动到下一行同列中的项目。如果当前项目的父代没有更多可浏览的行,则光标会移动到父代之后的同级项目第一行中的相关项目。
向左通过折叠分支隐藏当前项目的子项目(如果存在)。
与左相同。
通过扩展分支来显示当前项目的子项目(如果存在)。
与右相同。
星号展开当前项目及其所有子项目(如果存在)。
上一页将光标向上移动一页。
下一页将光标下移一页。
首页将光标移动到模型中第一个顶层项第一行同列中的一个项。
结束将光标移至模型中最后一个顶层项最后一行同列中的项目。
F2在可编辑模型中,此键将打开当前项目以供编辑。使用 Escape 键可以取消编辑过程,并恢复对显示数据的任何更改。

提高性能

可以为视图处理的数据提供提示,以提高其在显示大量项目时的性能。对于要显示等高项的视图,一种方法是将uniformRowHeights 属性设置为 true。

另请参阅 QListView,QTreeWidget,视图类,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 插入。

访问函数:

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 的大小在页眉中发生变化时,都会调用该函数。oldSizenewSize 以像素为单位给出先前的大小和新的大小。

另请参见 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 &current, const QModelIndex &previous)

重实现:QAbstractItemView::currentChanged(const QModelIndex &current, 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 &region) 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 的项目及其所有子项扩展到给定的depthdepth 相对于给定的indexdepth 为 -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().

返回树形视图的页眉。

另请参阅 setHeader() 和QAbstractItemModel::headerData()。

[slot] void QTreeView::hideColumn(int column)

隐藏给定的column

注意: 此函数只能在模型初始化后调用,因为视图需要知道列数才能隐藏column

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

[override virtual protected] int QTreeView::horizontalOffset() const

重实现:QAbstractItemView::horizontalOffset() 常量。

返回树状视图中项目的水平偏移量。

请注意,树状视图使用水平标题部分的位置来确定视图中列的位置。

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

如果parent 中给定的row 第一列中的项目横跨所有列,则返回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()。

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

通知视图已从给定的parent 模型项中删除了从start 行到end 行(含 行)的记录。

[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 为真,则隐藏带有给定parentrow ,否则显示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::sizeHintheader() 和QHeaderView::resizeContentsPrecision()。

[slot] void QTreeView::sortByColumn(int column, Qt::SortOrder order)

根据给定的columnorder 中的值对模型排序。

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() 常量。

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

另请参阅 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 的项目在视口中占据的矩形。如果索引不可见或显式隐藏,返回的矩形无效。

[override virtual protected] QRegion QTreeView::visualRegionForSelection(const QItemSelection &selection) const

重实现:QAbstractItemView::visualRegionForSelection(const QItemSelection &selection) 常量。

从视口返回给定selection 中项目的矩形。

自 4.7 版起,返回的区域只包含与视口相交(或包含在视口中)的矩形。

© 2025 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.