本页内容

QHeaderView Class

QHeaderView 类为项目视图提供标题行或标题列。更多内容...

标题: #include <QHeaderView>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承自: QAbstractItemView

公共类型

enum ResizeMode { Interactive, Fixed, Stretch, ResizeToContents, Custom }

属性

公共函数

QHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
virtual ~QHeaderView()
bool cascadingSectionResizes() const
int count() const
Qt::Alignment defaultAlignment() const
int defaultSectionSize() const
int hiddenSectionCount() const
void hideSection(int logicalIndex)
bool highlightSections() const
bool isFirstSectionMovable() const
bool isSectionHidden(int logicalIndex) const
bool isSortIndicatorClearable() const
bool isSortIndicatorShown() const
int length() const
int logicalIndex(int visualIndex) const
int logicalIndexAt(const QPoint &pos) const
int logicalIndexAt(int position) const
int logicalIndexAt(int x, int y) const
int maximumSectionSize() const
int minimumSectionSize() const
void moveSection(int from, int to)
int offset() const
Qt::Orientation orientation() const
void resetDefaultSectionSize()
int resizeContentsPrecision() const
void resizeSection(int logicalIndex, int size)
void resizeSections(QHeaderView::ResizeMode mode)
bool restoreState(const QByteArray &state)
QByteArray saveState() const
int sectionPosition(int logicalIndex) const
QHeaderView::ResizeMode sectionResizeMode(int logicalIndex) const
int sectionSize(int logicalIndex) const
int sectionSizeHint(int logicalIndex) const
int sectionViewportPosition(int logicalIndex) const
bool sectionsClickable() const
bool sectionsHidden() const
bool sectionsMovable() const
bool sectionsMoved() const
void setCascadingSectionResizes(bool enable)
void setDefaultAlignment(Qt::Alignment alignment)
void setDefaultSectionSize(int size)
void setFirstSectionMovable(bool movable)
void setHighlightSections(bool highlight)
void setMaximumSectionSize(int size)
void setMinimumSectionSize(int size)
void setResizeContentsPrecision(int precision)
void setSectionHidden(int logicalIndex, bool hide)
void setSectionResizeMode(QHeaderView::ResizeMode mode)
void setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)
void setSectionsClickable(bool clickable)
void setSectionsMovable(bool movable)
void setSortIndicator(int logicalIndex, Qt::SortOrder order)
void setSortIndicatorClearable(bool clearable)
void setSortIndicatorShown(bool show)
void setStretchLastSection(bool stretch)
void showSection(int logicalIndex)
Qt::SortOrder sortIndicatorOrder() const
int sortIndicatorSection() const
bool stretchLastSection() const
int stretchSectionCount() const
void swapSections(int first, int second)
int visualIndex(int logicalIndex) const
int visualIndexAt(int position) const

重新实现的公共函数

virtual void reset() override
virtual void setModel(QAbstractItemModel *model) override
virtual void setVisible(bool v) override
virtual QSize sizeHint() const override

公共槽

void headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast)
void setOffset(int offset)
void setOffsetToLastSection()
void setOffsetToSectionPosition(int visualSectionNumber)

信号

void geometriesChanged()
void sectionClicked(int logicalIndex)
void sectionCountChanged(int oldCount, int newCount)
void sectionDoubleClicked(int logicalIndex)
void sectionEntered(int logicalIndex)
void sectionHandleDoubleClicked(int logicalIndex)
void sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
void sectionPressed(int logicalIndex)
void sectionResized(int logicalIndex, int oldSize, int newSize)
void sortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
void sortIndicatorClearableChanged(bool clearable)

受保护函数

virtual void initStyleOption(QStyleOptionHeader *option) const
(since 6.0) virtual void initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const
virtual void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
virtual QSize sectionSizeFromContents(int logicalIndex) const

重新实现的受保护函数

virtual void currentChanged(const QModelIndex &current, const QModelIndex &old) override
virtual bool event(QEvent *e) override
virtual int horizontalOffset() const override
virtual void mouseDoubleClickEvent(QMouseEvent *e) override
virtual void mouseMoveEvent(QMouseEvent *e) override
virtual void mousePressEvent(QMouseEvent *e) override
virtual void mouseReleaseEvent(QMouseEvent *e) override
virtual void paintEvent(QPaintEvent *e) override
virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) override
virtual int verticalOffset() const override
virtual bool viewportEvent(QEvent *e) override

受保护的槽

void resizeSections()
void sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast)
void sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast)

详细说明

QHeaderView 用于显示项目视图(如QTableView 和QTreeView 类)中使用的标题。它取代了此前用于相同目的的 Qt3 的QHeader 类,但采用了 Qt 的模型/视图架构,以确保与项目视图类保持一致。

QHeaderView 类是模型/视图类之一,属于 Qt的模型/视图框架。

标题视图通过QAbstractItemModel::headerData()函数从模型获取各部分的数据。您可以使用QAbstractItemModel::setHeaderData()函数设置数据。

每个标题都拥有一个orientation()方法,以及由count()函数指定的多个部分。所谓“部分”,是指标题的某个区域——根据布局方向的不同,可以是行也可以是列。

可通过 `moveSection()` 和 `resizeSection()` 移动和调整各部分的大小;也可通过 `hideSection()` 和 `showSection()` 隐藏或显示各部分。

页眉的每个部分都由一个部分 ID 描述,该 ID 由其 section() 指定,并可位于页眉中的特定visualIndex() 位置。可以通过setSortIndicator() 为部分设置排序指示符;这表示关联的项视图中的项是否将按该部分指定的顺序进行排序。

对于水平标题,该部分相当于模型中的列;对于垂直标题,该部分相当于模型中的行。

移动标题部分

标题栏可以固定在原位,也可以通过setSectionsMovable() 使其可移动。可以通过setSectionsClickable() 使其可点击,并根据setSectionResizeMode() 具有调整大小的行为。

注意:双击 标题栏以调整部分大小的操作仅适用于可见行。

当用户移动部分时,标题行会触发sectionMoved();当用户调整部分大小时,会触发sectionResized();当用户点击鼠标时,会同时触发sectionClicked() 和sectionHandleDoubleClicked()。此外,标题行还会触发sectionCountChanged()。

您可以通过logicalIndex()和logicalIndexAt()函数识别一个分区,或者通过visualIndex()和visualIndexAt()函数根据其索引位置进行识别。如果分区被移动,视觉索引会发生变化,但逻辑索引不会改变。

外观

QTableWidget 和QTableView 会生成默认标题。若需显示标题,可使用setVisible()。

并非所有ItemDataRole都会对 QHeaderView 产生影响。若需绘制其他角色,可继承 QHeaderView 并重写paintEvent() 方法。QHeaderView 支持以下项目数据角色,除非它们与样式冲突(遵循桌面主题的样式可能会出现这种情况):

TextAlignmentRole、DisplayRole 、FontRole 、DecorationRole 、ForegroundRole 以及BackgroundRole 。

注意:每个 标题都会自行渲染各部分的数据,并不依赖于委托。因此,调用标题的 `setItemDelegate()` 函数将不会产生任何效果。

针对超大模型的特殊考虑

该标题视图每个部分占用 8 到 16 字节的内存。不过,自 Qt 6.9 起,只有当一个或多个部分被调整大小或重新排序时,才会使用这部分内存。 这意味着,只要不调用 swapSections 、resizeSection 、hideSection 、moveSection 以及stretchLastSection (启用该功能),即使模型包含数百万个部分,QHeaderView 也不会消耗成比例的、因而巨大的内存。 为避免用户操作触发此类调用,应将resize mode 设置为Fixed (切勿针对任何索引单独指定该值,否则将产生相反效果)。此外,还应通过禁用sectionsMovable 来防止用户移动部分。

安全注意事项

restoreState() 函数负责反序列化一个带版本的二进制数据块,该数据块描述了列顺序、分区大小、排序指示符以及相关的视图状态。 虽然会验证该格式的魔数、版本号和报告的总长度,但在将反序列化的视觉索引、逻辑索引以及排序指示符部分用于索引内部数据结构之前,不会将其与头部的实际分区计数进行比对。

仅向restoreState() 传递由saveState() 先前生成并由同一版本(或兼容版本)的应用程序持久化的QByteArray ,通常通过QSettings 进行过往返传输。 请勿使用来源未知或不可信的数据调用restoreState(),例如从网络下载的文件、已同步或共享的配置文件,或由其他可能已受损的应用程序提供的数据。

另请参阅 《模型/视图编程》、QListView 、QTableView 、QTreeView 、saveState() 以及restoreState()。

成员类型文档

enum QHeaderView::ResizeMode

调整大小模式指定了标题区域的行为。可以通过调用setSectionResizeMode()方法,对整个标题视图或单个区域进行设置。

常量值描述
QHeaderView::Interactive0用户可以调整该部分的大小。也可以通过调用 `resizeSection()` 方法以编程方式调整该部分的大小。该部分的大小默认设置为 `defaultSectionSize`。(另请参阅 `cascadingSectionResizes`。)
QHeaderView::Fixed2用户无法调整该区域的大小。该区域只能通过resizeSection() 方法以编程方式调整大小。该区域的大小默认设置为defaultSectionSize 。(另请参阅 。)
QHeaderView::Stretch1QHeaderView 将自动调整该部分的大小以填满可用空间。该大小既不能由用户更改,也不能通过编程方式更改。
QHeaderView::ResizeToContents3QHeaderView 将根据整个列或行的内容自动将该部分调整为最佳大小。用户或程序均无法更改其大小。(此值自 4.2 版起引入)

以下值已弃用:

常量值描述
QHeaderView::CustomFixed请改用 Fixed。

另请参阅 setSectionResizeMode()、stretchLastSection 和minimumSectionSize 。

属性文档

cascadingSectionResizes : bool

该属性控制当用户正在调整大小的部分达到其最小尺寸后,交互式调整大小是否会级联到后续部分

该属性仅影响将“Interactive ”设为调整大小模式的区域。

默认值为 false。

访问函数:

bool cascadingSectionResizes() const
void setCascadingSectionResizes(bool enable)

另请参阅 setSectionResizeMode()。

defaultAlignment : Qt::Alignment

此属性用于设置每个标题部分中文本的默认对齐方式

访问函数:

Qt::Alignment defaultAlignment() const
void setDefaultAlignment(Qt::Alignment alignment)

defaultSectionSize : int

该属性保存了标题部分在调整大小前的默认尺寸。

该属性仅影响重置模式为Interactive 或Fixed 的区域。

默认情况下,该属性的值取决于样式。因此,当样式发生变化时,该属性会随之更新。调用 setDefaultSectionSize() 将停止更新,调用 resetDefaultSectionSize() 将恢复默认行为。

访问函数:

int defaultSectionSize() const
void setDefaultSectionSize(int size)
void resetDefaultSectionSize()

另请参阅 setSectionResizeMode() 和minimumSectionSize 。

firstSectionMovable : bool

该属性控制用户是否可以移动第一列

该属性控制用户是否可以移动第一列。在QTreeView 中,第一列包含树结构,因此默认情况下不可移动,即使将setSectionsMovable (true)设置为true也是如此。

例如,对于没有树结构的扁平列表,可以通过调用此方法使其恢复可移动状态。在这种情况下,建议同时调用QTreeView::setRootIsDecorated(false)。

treeView->setRootIsDecorated(false);
treeView->header()->setFirstSectionMovable(true);

除非同时调用setSectionsMovable (true),否则将其设置为true不会产生任何效果。

访问函数:

bool isFirstSectionMovable() const
void setFirstSectionMovable(bool movable)

另请参阅 setSectionsMovable()。

highlightSections : bool

该属性用于控制是否突出显示包含所选项目的部分

默认情况下,此属性的值为false 。

访问函数:

bool highlightSections() const
void setHighlightSections(bool highlight)

maximumSectionSize : int

该属性用于指定标题部分的最大大小。

最大分区大小是指允许的最大分区大小。该属性的默认值为 1048575,这也是分区可能达到的最大大小。将 maximum 设置为 -1 将把该值重置为最大分区大小。

除 stretch 属性外,所有resize modes

Access 函数都会遵循此属性:

int maximumSectionSize() const
void setMaximumSectionSize(int size)

另请参阅 setSectionResizeMode() 和defaultSectionSize 。

minimumSectionSize : int

该属性用于指定标题部分的最小尺寸。

最小分区大小是指允许的最小分区大小。如果将最小分区大小设置为 -1,QHeaderView 将使用font metrics 中的大小。

所有resize modes 都会遵循此属性。

访问函数:

int minimumSectionSize() const
void setMinimumSectionSize(int size)

另请参阅 setSectionResizeMode() 和defaultSectionSize 。

sectionsClickable : bool

如果标题可点击,则返回true ;否则返回false 。可点击的标题可用于允许用户更改视图中与该标题相关的数据的显示方式。

访问函数:

bool sectionsClickable() const
void setSectionsClickable(bool clickable)

另请参阅 sectionPressed() 和setSortIndicatorShown()。

sectionsMovable : bool

如果sectionsMovable 为 true,用户可以移动标题部分;否则,这些部分将固定在原位。

当与QTreeView 结合使用时,默认情况下第一列不可移动(因为它包含树结构)。您可以通过setFirstSectionMovable (true)使其可移动。

访问函数:

bool sectionsMovable() const
void setSectionsMovable(bool movable)

另请参阅 sectionMoved() 和setFirstSectionMovable()。

showSortIndicator : bool

该属性控制排序指示符是否显示

默认情况下,此属性的值为false 。

访问函数:

bool isSortIndicatorShown() const
void setSortIndicatorShown(bool show)

另请参阅 setSectionsClickable()。

[since 6.1] sortIndicatorClearable : bool

该属性控制是否可以通过多次单击某个分区来清除排序指示器

该属性控制用户是否可以通过多次点击某个部分来清除该部分的排序标记。通常情况下,点击某个部分只会更改该部分的排序顺序。将该属性设置为 true 时,在升序和降序之间切换后,排序标记将被清除;这通常会恢复模型的原始排序。

除非 `sectionsClickable()` 也为 `true`(这是某些视图的默认设置,例如 `QTableView`,或者在使视图可排序时自动设置,例如通过调用 `QTreeView::setSortingEnabled`),否则将此属性设置为 `true` 不会产生任何效果。

该枚举类型在 Qt 6.1 中引入。

访问函数:

bool isSortIndicatorClearable() const
void setSortIndicatorClearable(bool clearable)

通知器信号:

void sortIndicatorClearableChanged(bool clearable)

stretchLastSection : bool

该属性用于指定页眉中最后一个可见部分是否占据所有可用空间

默认值为 false。

注意: QTreeView 提供的水平标题栏在 配置时会将此属性设置为 true,以确保视图不会浪费分配给标题栏的任何空间。如果此值设置为 true,则该属性将覆盖标题栏中最后一个部分设置的调整大小模式。

访问函数:

bool stretchLastSection() const
void setStretchLastSection(bool stretch)

另请参阅 setSectionResizeMode()。

成员函数文档

[explicit] QHeaderView::QHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)

使用给定的orientation 和parent 创建一个新的泛型头文件。

[virtual noexcept] QHeaderView::~QHeaderView()

删除标题。

int QHeaderView::count() const

返回页眉中的分节符数量。

另请参阅 sectionCountChanged() 和length()。

[override virtual protected] void QHeaderView::currentChanged(const QModelIndex &current, const QModelIndex &old)

重写了:QAbstractItemView::currentChanged (const QModelIndex &current, const QModelIndex &previous)。

[override virtual protected] bool QHeaderView::event(QEvent *e)

重写了:QAbstractItemView::event(QEvent *event)。

[signal] void QHeaderView::geometriesChanged()

当标头的几何形状发生变化时,会发出此信号。

[slot] void QHeaderView::headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast)

使用给定的orientation 更新已更改的标题部分,范围从logicalFirst 到logicalLast (含)。

int QHeaderView::hiddenSectionCount() const

返回标题中已被隐藏的章节数量。

另请参阅 setSectionHidden() 和isSectionHidden()。

void QHeaderView::hideSection(int logicalIndex)

隐藏由logicalIndex 指定的部分。

另请参阅 showSection()、isSectionHidden()、hiddenSectionCount() 以及setSectionHidden()。

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

重新实现:QAbstractItemView::horizontalOffset() const。

返回标题的水平偏移量。对于垂直标题,该值为 0。

另请参阅 offset()。

[virtual protected] void QHeaderView::initStyleOption(QStyleOptionHeader *option) const

使用QHeaderView 中的值初始化option 。当子类需要QStyleOptionHeader ,但又不想自己填写所有信息时,此方法非常有用。

另请参阅 QStyleOption::initFrom() 和initStyleOptionForIndex()。

[virtual protected, since 6.0] void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const

初始化来自指定logicalIndex 的样式option 。该函数由paintSection 的默认实现调用,且在调用initStyleOption 之后执行。

该函数在 Qt 6.0 中引入。

另请参阅 paintSection() 和initStyleOption()。

bool QHeaderView::isSectionHidden(int logicalIndex) const

如果由 `logicalIndex ` 指定的章节被明确隐藏,则返回 `true `;否则返回 `false`。

另请参阅 hideSection()、showSection()、setSectionHidden() 以及hiddenSectionCount()。

int QHeaderView::length() const

返回沿标头方向的长度。

另请参阅 sizeHint()、setSectionResizeMode(),以及offset()。

int QHeaderView::logicalIndex(int visualIndex) const

返回位于给定visualIndex 位置的章节的logicalIndex,如果visualIndex < 0或visualIndex >=QHeaderView::count(),则返回-1。

请注意,visualIndex 不受隐藏部分的影响。

另请参阅 visualIndex() 和sectionPosition()。

int QHeaderView::logicalIndexAt(const QPoint &pos) const

返回位于pos 中指定位置的章节的逻辑索引。如果标题为水平排列,则使用x坐标;否则,使用y坐标来查找逻辑索引。

另请参阅 sectionPosition()。

int QHeaderView::logicalIndexAt(int position) const

返回视口内涵盖给定position 的区域。

另请参阅 visualIndexAt() 和isSectionHidden()。

int QHeaderView::logicalIndexAt(int x, int y) const

返回给定坐标处该部分的逻辑索引。如果标题为水平排列,则使用x ;否则,使用y 来查找逻辑索引。

[override virtual protected] void QHeaderView::mouseDoubleClickEvent(QMouseEvent *e)

重写了:QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)。

[override virtual protected] void QHeaderView::mouseMoveEvent(QMouseEvent *e)

重写:QAbstractItemView::mouseMoveEvent(QMouseEvent *event)。

[override virtual protected] void QHeaderView::mousePressEvent(QMouseEvent *e)

重写了:QAbstractItemView::mousePressEvent(QMouseEvent *event)。

[override virtual protected] void QHeaderView::mouseReleaseEvent(QMouseEvent *e)

重写了:QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)。

void QHeaderView::moveSection(int from, int to)

将位于视觉索引from 的部分移动到视觉索引to 。

另请参阅 sectionsMoved()。

int QHeaderView::offset() const

返回标题的偏移量:即标题最左侧(对于垂直标题则为最顶端)的可见像素。

另请参阅 setOffset()。

Qt::Orientation QHeaderView::orientation() const

返回标题的方位。

另请参阅 Qt::Orientation 。

[override virtual protected] void QHeaderView::paintEvent(QPaintEvent *e)

重写了:QAbstractScrollArea::paintEvent(QPaintEvent *event)。

[virtual protected] void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const

使用给定的painter 和rect ,绘制由给定的logicalIndex 指定的区域。

通常情况下,您无需调用此函数。

[override virtual] void QHeaderView::reset()

重写了:QAbstractItemView::reset()。

int QHeaderView::resizeContentsPrecision() const

返回QHeaderView 在ResizeToContents 上的计算精度。

另请参阅 setResizeContentsPrecision() 和setSectionResizeMode()。

void QHeaderView::resizeSection(int logicalIndex, int size)

将由logicalIndex 指定的区域调整为size (以像素为单位)。size参数的值必须大于或等于零。但不建议将size设为零。在这种情况下,应改用hideSection 。

另请参阅 sectionResized()、sectionSize() 和hideSection()。

[protected slot] void QHeaderView::resizeSections()

根据各区域的大小提示调整其尺寸。通常情况下,您无需调用此函数。

注意:此 插槽已被重载。要连接到此插槽:

// Connect using qOverload:
connect(sender, &SenderClass::signal,
        headerView, qOverload<>(&QHeaderView::resizeSections));

// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
        headerView, [receiver = headerView]() { receiver->resizeSections(); });
有关更多示例和方法,请参阅“连接到重载的插槽”。

void QHeaderView::resizeSections(QHeaderView::ResizeMode mode)

根据给定的mode 调整各部分的大小,并忽略当前的调整大小模式。

另请参阅 sectionResized()。

bool QHeaderView::restoreState(const QByteArray &state)

恢复此标题视图的state 。如果状态已恢复,该函数返回true ;否则返回false。

另请参阅 saveState()。

QByteArray QHeaderView::saveState() const

保存此标题视图的当前状态。

要恢复已保存的状态,请将返回值传递给restoreState()。

另请参阅 restoreState()。

[signal] void QHeaderView::sectionClicked(int logicalIndex)

当点击某个部分时,会触发此信号。该部分的逻辑索引由logicalIndex 指定。

请注意,sectionPressed 信号也会被触发。

另请参阅 setSectionsClickable() 和sectionPressed()。

[signal] void QHeaderView::sectionCountChanged(int oldCount, int newCount)

当分段数量发生变化时(即添加或删除分段时),会发出此信号。原始计数由oldCount 指定,新计数由newCount 指定。

另请参阅 count()、length() 和headerDataChanged()。

[signal] void QHeaderView::sectionDoubleClicked(int logicalIndex)

当双击某个部分时,会发出此信号。该部分的逻辑索引由logicalIndex 指定。

另请参阅 setSectionsClickable()。

[signal] void QHeaderView::sectionEntered(int logicalIndex)

当光标移至该部分上方并按下鼠标左键时,会发出此信号。该部分的逻辑索引由logicalIndex 指定。

另请参阅 setSectionsClickable() 和sectionPressed()。

[signal] void QHeaderView::sectionHandleDoubleClicked(int logicalIndex)

当双击某个部分时,会发出此信号。该部分的逻辑索引由logicalIndex 指定。

另请参阅 setSectionsClickable()。

[signal] void QHeaderView::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)

当一个部分被移动时,会发出此信号。该部分的逻辑索引由logicalIndex 指定,旧索引由oldVisualIndex 指定,新索引位置由newVisualIndex 指定。

另请参阅 moveSection()。

int QHeaderView::sectionPosition(int logicalIndex) const

返回给定logicalIndex 的段落位置,如果该段落被隐藏,则返回-1。该位置以像素为单位,从第一个可见项的左上角到该项的左上角(该项的logicalIndex 属性为true)进行测量。对于水平标题,测量沿x轴进行;对于垂直标题,测量沿y轴进行。

另请参阅 sectionViewportPosition()。

[signal] void QHeaderView::sectionPressed(int logicalIndex)

当某个部分被按下时,会发出此信号。该部分的逻辑索引由logicalIndex 指定。

另请参阅 setSectionsClickable()。

QHeaderView::ResizeMode QHeaderView::sectionResizeMode(int logicalIndex) const

返回适用于由给定的logicalIndex 指定的部分的调整大小模式。

另请参阅 setSectionResizeMode()。

[signal] void QHeaderView::sectionResized(int logicalIndex, int oldSize, int newSize)

当一个分区被调整大小时,会发出此信号。分区的逻辑编号由logicalIndex 指定,旧大小由oldSize 指定,新大小由newSize 指定。

另请参阅 resizeSection()。

int QHeaderView::sectionSize(int logicalIndex) const

返回给定logicalIndex 的宽度(如果是垂直标题,则返回高度)。

另请参阅 length()、setSectionResizeMode() 和defaultSectionSize()。

[virtual protected] QSize QHeaderView::sectionSizeFromContents(int logicalIndex) const

返回由给定的logicalIndex 指定的段的内容大小。

另请参阅 defaultSectionSize()。

int QHeaderView::sectionSizeHint(int logicalIndex) const

返回适用于由logicalIndex 指定的段落的合适大小提示。

Qt::SizeHintRole

另请参阅 sizeHint()、defaultSectionSize()、minimumSectionSize() 以及maximumSectionSize()。

int QHeaderView::sectionViewportPosition(int logicalIndex) const

返回给定logicalIndex 的截面视口位置。

如果该截面被隐藏,则返回值为未定义。

另请参阅 sectionPosition() 和isSectionHidden()。

[protected slot] void QHeaderView::sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast)

当从parent 中移除章节时,会调用此插槽。logicalFirst 和logicalLast 分别表示被移除的章节所在的位置。

如果仅移除一个分区,则logicalFirst 和logicalLast 的值将相同。

bool QHeaderView::sectionsClickable() const

返回 sectionsClickable。

注意: 这是属性 sectionsClickable 的获取器 函数。

另请参阅 ` setSectionsClickable()`。

bool QHeaderView::sectionsHidden() const

如果页眉中的部分已被隐藏,则返回true ;否则返回false;

另请参阅 setSectionHidden()。

[protected slot] void QHeaderView::sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast)

当向 `parent` 中插入章节时,会调用此插槽。索引 `logicalFirst ` 和 `logicalLast ` 表示新章节的插入位置。

如果仅插入一个章节,则logicalFirst 和logicalLast 将相同。

bool QHeaderView::sectionsMovable() const

返回 sectionsMovable。

注意: 这是属性 sectionsMovable 的获取器 函数。

另请参阅 setSectionsMovable()。

bool QHeaderView::sectionsMoved() const

如果标头中的部分已被移动,则返回true ;否则返回false;

另请参阅 moveSection()。

[override virtual] void QHeaderView::setModel(QAbstractItemModel *model)

重写了:QAbstractItemView::setModel(QAbstractItemModel *model)。

[slot] void QHeaderView::setOffset(int offset)

将标题的偏移量设置为offset 。

另请参阅 offset() 和length()。

[slot] void QHeaderView::setOffsetToLastSection()

设置偏移量,以使最后一个部分可见。

另请参阅 setOffset()、sectionPosition() 和setOffsetToSectionPosition()。

[slot] void QHeaderView::setOffsetToSectionPosition(int visualSectionNumber)

将偏移量设置为给定visualSectionNumber 处该部分的起始位置。visualSectionNumber 是指在不考虑hiddenSections时实际可见的部分。这并不总是与visualIndex()的结果相同。

另请参阅 setOffset() 和sectionPosition()。

void QHeaderView::setResizeContentsPrecision(int precision)

设置在使用 `ResizeToContents ` 时,QHeaderView 计算尺寸的精度。较低的数值会提供精度较低但速度较快的自动调整大小功能,而较高的数值则会提供更精确的调整大小效果,但速度可能较慢。

precision 指定的数值表示在计算首选尺寸时应考虑的分区数量。

默认值为 1000,这意味着具有自动调整大小的水平列在计算时,最多会考虑 1000 行。

特殊值 0 表示仅考虑可见区域;特殊值 -1 则表示考虑所有元素。

该值用于QTableView::sizeHintForColumn()、QTableView::sizeHintForRow() 和QTreeView::sizeHintForColumn() 函数中。重写这些函数可能会导致该功能失效。

另请参阅 resizeContentsPrecision()、setSectionResizeMode()、resizeSections()、QTableView::sizeHintForColumn()、QTableView::sizeHintForRow()以及QTreeView::sizeHintForColumn()。

void QHeaderView::setSectionHidden(int logicalIndex, bool hide)

如果hide 为真,则由logicalIndex 指定的章节将被隐藏;否则该章节将显示。

另请参阅 isSectionHidden() 和hiddenSectionCount()。

void QHeaderView::setSectionResizeMode(QHeaderView::ResizeMode mode)

将标题栏调整大小的限制设置为由给定的mode 中描述的限制。

另请参阅 sectionResizeMode()、length() 和sectionResized()。

void QHeaderView::setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)

将由标题中logicalIndex 指定的部分的调整大小限制设置为由给定的mode 所描述的限制。调用此函数时,逻辑索引应已存在。

注意: 如果stretchLastSection 属性设置为 true,则此 设置对最后一个部分将被忽略。这是QTreeView 提供的水平标题栏的默认行为。

另请参阅 setStretchLastSection() 和resizeContentsPrecision()。

void QHeaderView::setSectionsClickable(bool clickable)

将sectionsClickable 设置为clickable 。

注意: 这是属性sectionsClickable 的设置 函数。

另请参阅 sectionsClickable()。

void QHeaderView::setSectionsMovable(bool movable)

将sectionsMovable 设置为movable 。

注意: 这是属性sectionsMovable 的设置 函数。

另请参阅 sectionsMovable()。

[override virtual protected] void QHeaderView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)

重写:QAbstractItemView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)。

根据指定的flags ,从给定的rect 中选择项目。

基类的实现不执行任何操作。

void QHeaderView::setSortIndicator(int logicalIndex, Qt::SortOrder order)

将由给定的logicalIndex 指定的部分的排序指示符设置为order 指定的方向,并从其他显示该排序指示符的部分中移除该指示符。

logicalIndex 该参数可设为 -1,此时将不显示任何排序标记,模型将恢复其自然、未排序的顺序。请注意,并非所有模型都支持此功能,在此情况下甚至可能导致模型崩溃。

另请参阅 sortIndicatorSection() 和sortIndicatorOrder()。

[override virtual] void QHeaderView::setVisible(bool v)

重新实现了属性QWidget::visible 的访问函数。

void QHeaderView::showSection(int logicalIndex)

显示由logicalIndex 指定的章节。

另请参阅 hideSection()、isSectionHidden()、hiddenSectionCount() 以及setSectionHidden()。

[override virtual] QSize QHeaderView::sizeHint() const

重新实现了:QAbstractScrollArea::sizeHint() const。

返回该头文件的合适大小提示。

另请参阅 sectionSizeHint()。

[signal] void QHeaderView::sortIndicatorChanged(int logicalIndex, Qt::SortOrder order)

当包含排序指示符或指定排序顺序的段发生变化时,会发出此信号。该段的逻辑索引由logicalIndex 指定,排序顺序由order 指定。

另请参阅 setSortIndicator()。

Qt::SortOrder QHeaderView::sortIndicatorOrder() const

返回排序指示符的顺序。如果没有任何部分具有排序指示符,则该函数的返回值未定义。

另请参阅 setSortIndicator() 和sortIndicatorSection()。

int QHeaderView::sortIndicatorSection() const

返回带有排序指示符的章节的逻辑索引。默认情况下,该索引为 0。

另请参阅 setSortIndicator()、sortIndicatorOrder() 和setSortIndicatorShown()。

int QHeaderView::stretchSectionCount() const

返回设置为“拉伸”调整大小模式的区域数量。在视图中,这可用于判断当视图的几何形状发生变化时,headerview 是否需要调整这些区域的大小。

另请参阅 stretchLastSection 。

void QHeaderView::swapSections(int first, int second)

将视觉索引为first 的部分与视觉索引为second 的部分互换。

另请参阅 moveSection()。

[override virtual protected] int QHeaderView::verticalOffset() const

重新实现了:QAbstractItemView::verticalOffset() const。

返回标题的垂直偏移量。对于水平标题,该值为 0。

另请参阅 offset()。

[override virtual protected] bool QHeaderView::viewportEvent(QEvent *e)

重写了:QAbstractItemView::viewportEvent(QEvent *event)。

int QHeaderView::visualIndex(int logicalIndex) const

返回由给定的logicalIndex 指定的节的视觉索引位置,否则返回-1。

隐藏的章节仍然具有有效的视觉索引。

另请参阅 logicalIndex()。

int QHeaderView::visualIndexAt(int position) const

返回视口内覆盖给定坐标position 的区域的视觉索引。

另请参阅 logicalIndexAt()。

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