PySide6.QtWidgets.QHeaderView¶
- class QHeaderView¶
QHeaderView
类为项目视图提供了标题行或标题列。更多…概要¶
属性¶
cascadingSectionResizesᅟ
- 当用户调整大小的部分达到其最小尺寸时,是否将交互式调整大小级联到后续部分defaultAlignmentᅟ
- 每个标题部分中文本的默认对齐方式defaultSectionSizeᅟ
- 调整大小前标题部分的默认大小firstSectionMovableᅟ
- 第一列是否可以被用户移动highlightSectionsᅟ
- 是否高亮显示包含选定项目的部分maximumSectionSizeᅟ
- 头部部分的最大大小minimumSectionSizeᅟ
- 标题部分的最小尺寸showSortIndicatorᅟ
- 是否显示排序指示器sortIndicatorClearableᅟ
- 是否可以通过多次点击某个部分来清除排序指示器stretchLastSectionᅟ
- 标题中最后一个可见部分是否占据所有可用空间
方法¶
def
__init__()
def
count()
def
hideSection()
def
initialize()
def
length()
def
logicalIndex()
def
logicalIndexAt()
def
moveSection()
def
offset()
def
orientation()
def
resizeSection()
def
resizeSections()
def
restoreState()
def
saveState()
def
sectionSize()
def
sectionsHidden()
def
sectionsMoved()
def
showSection()
def
swapSections()
def
visualIndex()
def
visualIndexAt()
虚拟方法¶
插槽¶
信号¶
注意
本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。
详细描述¶
一个
QHeaderView
显示用于项目视图的标题,例如QTableView
和QTreeView
类。它取代了之前用于相同目的的Qt3的QHeader
类,但使用Qt的模型/视图架构以与项目视图类保持一致。QHeaderView
类是 Model/View Classes 之一,并且是 Qt 的 model/view framework 的一部分。头部使用QAbstractItemModel::headerData()函数从模型中获取每个部分的数据。您可以使用QAbstractItemModel::setHeaderData()来设置数据。
每个标题都有一个
orientation()
和多个部分,由count()
函数给出。一个部分指的是标题的一部分——根据方向的不同,可以是行或列。可以使用
moveSection()
和resizeSection()
来移动和调整部分的大小;也可以使用hideSection()
和showSection()
来隐藏和显示部分。标题的每个部分由一个部分ID描述,由其section()指定,并且可以在标题中的特定
visualIndex()
位置找到。一个部分可以使用setSortIndicator()
设置排序指示器;这表示关联项视图中的项目是否将按照该部分给出的顺序进行排序。对于水平标题,该部分相当于模型中的一列,而对于垂直标题,该部分相当于模型中的一行。
移动标题部分¶
标题可以固定在原位,或者通过
setSectionsMovable()
使其可移动。可以通过setSectionsClickable()
使其可点击,并且根据setSectionResizeMode()
具有调整大小的行为。注意
双击标题以调整部分大小仅适用于可见行。
如果用户移动了一个部分,头部将发出
sectionMoved()
;如果用户调整了一个部分的大小,头部将发出sectionResized()
;以及响应鼠标点击时,头部将发出sectionClicked()
和sectionHandleDoubleClicked()
。头部还会发出sectionCountChanged()
。您可以使用
logicalIndex()
和logicalIndexAt()
函数来识别一个部分,或者通过其索引位置使用visualIndex()
和visualIndexAt()
函数。如果部分被移动,视觉索引会改变,但逻辑索引不会改变。外观¶
QTableWidget
和QTableView
创建默认的头部。如果你想让头部可见,你可以使用setVisible()
。并非所有的ItemDataRoles都会对
QHeaderView
产生影响。如果你需要绘制其他角色,你可以子类化QHeaderView
并重新实现paintEvent()
。QHeaderView
尊重以下项目数据角色,除非它们与样式冲突(这可能会发生在遵循桌面主题的样式中):文本对齐角色、显示角色、字体角色、装饰角色、前景角色和背景角色。
注意
每个标题都自行渲染其部分的数据,并不依赖于委托。因此,调用标题的
setItemDelegate()
函数将不会有任何效果。- class ResizeMode¶
调整大小模式指定了标题部分的行为。它可以在整个标题视图上设置,也可以在各个部分上使用
setSectionResizeMode()
进行设置。常量
描述
QHeaderView.Interactive
用户可以调整部分的大小。部分也可以通过使用
resizeSection()
以编程方式调整大小。部分大小默认为defaultSectionSize
。(另请参见cascadingSectionResizes
。)QHeaderView.Fixed
用户无法调整部分的大小。该部分只能通过编程方式使用
resizeSection()
来调整大小。部分大小默认为defaultSectionSize
。QHeaderView.Stretch
QHeaderView
将自动调整部分的大小以填充可用空间。用户或程序无法更改大小。QHeaderView.ResizeToContents
QHeaderView
将根据整个列或行的内容自动调整部分到其最佳大小。用户或程序无法更改此大小。(此值在4.2中引入)以下值已过时:
常量
描述
QHeaderView.Custom
请改用Fixed。
注意
当使用
from __feature__ import true_property
时,属性可以直接使用,否则通过访问器函数使用。- property cascadingSectionResizesᅟ: bool¶
此属性决定当用户调整大小的部分达到其最小尺寸时,是否将交互式调整大小级联到后续部分。
此属性仅影响将
Interactive
作为其调整大小模式的部分。默认值为 false。
- Access functions:
- property defaultAlignmentᅟ: Combination of Qt.AlignmentFlag¶
此属性保存每个标题部分中文本的默认对齐方式。
- Access functions:
- property defaultSectionSizeᅟ: int¶
此属性保存调整大小前标题部分的默认大小。
此属性仅影响将
Interactive
或Fixed
作为其调整大小模式的部分。默认情况下,此属性的值取决于样式。因此,当样式更改时,此属性会随之更新。调用 setDefaultSectionSize() 会停止更新,调用 resetDefaultSectionSize() 将恢复默认行为。
- Access functions:
- property firstSectionMovableᅟ: bool¶
此属性保存用户是否可以移动第一列。
此属性控制用户是否可以移动第一列。在
QTreeView
中,第一列包含树结构,因此默认情况下不可移动,即使在setSectionsMovable
(true)之后也是如此。它可以再次变为可移动的,例如在没有树结构的平面列表的情况下,通过调用此方法。在这种情况下,建议同时调用
setRootIsDecorated
(false)。treeView->setRootIsDecorated(false); treeView->header()->setFirstSectionMovable(true);
将其设置为true没有效果,除非同时调用
setSectionsMovable
(true)。另请参阅
- Access functions:
- property highlightSectionsᅟ: bool¶
此属性用于确定是否突出显示包含选定项的部分。
默认情况下,此属性为
false
。- Access functions:
- property maximumSectionSizeᅟ: int¶
此属性保存标题部分的最大大小。
最大部分大小是允许的最大部分大小。此属性的默认值为1048575,这也是一个部分的最大可能大小。将最大值设置为-1将把值重置为最大部分大小。
除了拉伸之外,所有
resize modes
都遵循此属性。- Access functions:
- property minimumSectionSizeᅟ: int¶
此属性保存标题部分的最小大小。
最小部分大小是允许的最小部分大小。如果最小部分大小设置为-1,
QHeaderView
将使用font metrics
大小。此属性由所有
resize modes
遵守。- Access functions:
- property sectionsClickableᅟ: bool¶
如果标题是可点击的,则保持
true
;否则保持false
。可点击的标题可以设置为允许用户更改与标题相关的视图中的数据表示。- Access functions:
- property sectionsMovableᅟ: bool¶
如果
sectionsMovable
为 true,用户可移动标题部分;否则它们将固定在原位。当与
QTreeView
结合使用时,默认情况下,第一列是不可移动的(因为它包含树结构)。你可以通过setFirstSectionMovable
(true)使其可移动。- Access functions:
- property showSortIndicatorᅟ: bool¶
此属性保存是否显示排序指示器。
默认情况下,此属性为
false
。- Access functions:
- property sortIndicatorClearableᅟ: bool¶
此属性表示是否可以通过多次点击某个部分来清除排序指示器。
此属性控制用户是否能够通过多次点击某个部分来移除该部分的排序指示器。通常,点击某个部分只会改变该部分的排序顺序。通过将此属性设置为true,排序指示器将在交替到升序和降序后被清除;这通常会恢复模型的原始排序。
将此属性设置为true不会产生任何效果,除非
sectionsClickable()
也为true(这是某些视图的默认设置,例如QTableView
,或者在使视图可排序时自动设置,例如通过调用setSortingEnabled
)。- Access functions:
- property stretchLastSectionᅟ: bool¶
此属性表示标题中的最后一个可见部分是否占据所有可用空间。
默认值为 false。
注意
由
QTreeView
提供的水平标题通过将此属性设置为true进行配置,确保视图不会浪费为其标题分配的任何空间。如果此值设置为true,此属性将覆盖标题中最后一部分设置的调整大小模式。- Access functions:
- __init__(orientation[, parent=None])¶
- Parameters:
orientation –
Orientation
parent –
QWidget
使用给定的
orientation
和parent
创建一个新的通用头部。- cascadingSectionResizes()¶
- Return type:
布尔
属性
cascadingSectionResizesᅟ
的获取器。- count()¶
- Return type:
整数
返回头部中的部分数量。
- defaultAlignment()¶
- Return type:
AlignmentFlag
的组合
属性
defaultAlignmentᅟ
的获取器。- defaultSectionSize()¶
- Return type:
整数
属性
defaultSectionSizeᅟ
的获取器。- geometriesChanged()¶
当标题的几何形状发生变化时,会发出此信号。
- headerDataChanged(orientation, logicalFirst, logicalLast)¶
- Parameters:
orientation –
Orientation
logicalFirst – int
logicalLast – int
使用给定的
orientation
更新更改的标题部分,从logicalFirst
到logicalLast
(包括这两个值)。- Return type:
整数
返回已隐藏的标题中的部分数量。
- hideSection(logicalIndex)¶
- Parameters:
logicalIndex – int
隐藏由
logicalIndex
指定的部分。- highlightSections()¶
- Return type:
布尔
属性
highlightSectionsᅟ
的获取器。- initStyleOption(option)¶
- Parameters:
选项 –
QStyleOptionHeader
使用此
QHeaderView
中的值初始化option
。当子类需要QStyleOptionHeader
但不想自己填写所有信息时,此方法非常有用。- initStyleOptionForIndex(option, logicalIndex)¶
- Parameters:
option –
QStyleOptionHeader
logicalIndex – int
从指定的
logicalIndex
初始化样式option
。此函数由paintSection
的默认实现在调用initStyleOption
后调用。- initialize()¶
- initializeSections()¶
- initializeSections(start, end)
- Parameters:
start – int
end – 整数
- isFirstSectionMovable()¶
- Return type:
布尔
属性
firstSectionMovableᅟ
的获取器。- isSectionHidden(logicalIndex)¶
- Parameters:
logicalIndex – int
- Return type:
布尔
如果由
logicalIndex
指定的部分对用户显式隐藏,则返回true
;否则返回false
。- isSortIndicatorClearable()¶
- Return type:
布尔
属性
sortIndicatorClearableᅟ
的获取器。- isSortIndicatorShown()¶
- Return type:
布尔
属性
showSortIndicatorᅟ
的获取器。- length()¶
- Return type:
整数
返回沿着标题方向的长度。
另请参阅
sizeHint()
setSectionResizeMode()
offset()
- logicalIndex(visualIndex)¶
- Parameters:
visualIndex – int
- Return type:
整数
返回给定
visualIndex
位置的逻辑索引,如果visualIndex
< 0 或visualIndex
>=count()
,则返回-1。请注意,
visualIndex
不受隐藏部分的影响。返回在
pos
给定位置处的部分的逻辑索引。如果标题是水平的,将使用x坐标,否则将使用y坐标来找到逻辑索引。另请参阅
- logicalIndexAt(position)
- Parameters:
position – int
- Return type:
整数
返回视口中覆盖给定
position
的部分。- logicalIndexAt(x, y)
- Parameters:
x – 整数
y – 整数
- Return type:
整数
返回给定坐标处部分的逻辑索引。如果标题是水平的,将使用
x
,否则将使用y
来查找逻辑索引。- maximumSectionSize()¶
- Return type:
整数
属性
maximumSectionSizeᅟ
的获取器。- minimumSectionSize()¶
- Return type:
整数
属性
minimumSectionSizeᅟ
的获取器。- moveSection(from, to)¶
- Parameters:
from – 整数
to – int
将视觉索引
from
处的部分移动到视觉索引to
处。另请参阅
- offset()¶
- Return type:
整数
返回标题的偏移量:这是标题最左侧(或垂直标题的最顶部)可见的像素。
另请参阅
返回标题的方向。
另请参阅
方向
- paintSection(painter, rect, logicalIndex)¶
使用给定的
painter
和rect
绘制由指定的logicalIndex
所定义的部分。通常,您不需要调用此函数。
- resetDefaultSectionSize()¶
属性
defaultSectionSizeᅟ
的重置功能。- resizeContentsPrecision()¶
- Return type:
整数
返回
QHeaderView
在ResizeToContents
上计算的精确度。- resizeSection(logicalIndex, size)¶
- Parameters:
logicalIndex – int
size – int
将
logicalIndex
指定的部分调整为size
,以像素为单位测量。size参数必须是一个大于或等于零的值。然而,不建议使用等于零的大小。在这种情况下,应使用hideSection
代替。- resizeSections()¶
根据它们的大小提示调整部分的大小。通常,您不需要调用此函数。
- resizeSections(mode)
- Parameters:
mode –
ResizeMode
根据给定的
mode
调整部分的大小,忽略当前的调整模式。另请参阅
- restoreState(state)¶
- Parameters:
状态 –
QByteArray
- Return type:
布尔
恢复此标题视图的
state
。如果状态已恢复,此函数返回true
;否则返回false。另请参阅
- saveState()¶
- Return type:
保存此标题视图的当前状态。
要恢复保存的状态,请将返回值传递给
restoreState()
。另请参阅
- sectionClicked(logicalIndex)¶
- Parameters:
logicalIndex – int
当点击一个部分时,会发出此信号。该部分的逻辑索引由
logicalIndex
指定。请注意,
sectionPressed
信号也会被发出。- sectionCountChanged(oldCount, newCount)¶
- Parameters:
oldCount – int
newCount – int
当部分数量发生变化时,即添加或删除部分时,会发出此信号。原始数量由
oldCount
指定,新数量由newCount
指定。- sectionDoubleClicked(logicalIndex)¶
- Parameters:
logicalIndex – int
当双击一个部分时,会发出此信号。该部分的逻辑索引由
logicalIndex
指定。- sectionEntered(logicalIndex)¶
- Parameters:
logicalIndex – int
当光标移动到该部分并按下鼠标左键时,会发出此信号。该部分的逻辑索引由
logicalIndex
指定。- sectionHandleDoubleClicked(logicalIndex)¶
- Parameters:
logicalIndex – int
当双击一个部分时,会发出此信号。该部分的逻辑索引由
logicalIndex
指定。- sectionMoved(logicalIndex, oldVisualIndex, newVisualIndex)¶
- Parameters:
logicalIndex – int
oldVisualIndex – int
newVisualIndex – int
当某个部分被移动时,会发出此信号。该部分的逻辑索引由
logicalIndex
指定,旧索引由oldVisualIndex
指定,新索引位置由newVisualIndex
指定。另请参阅
- sectionPosition(logicalIndex)¶
- Parameters:
logicalIndex – int
- Return type:
整数
返回给定
logicalIndex
的节位置,如果该节被隐藏,则返回-1。位置是从第一个可见项的左上角到具有logicalIndex
的项的左上角以像素为单位测量的。对于水平标题,测量沿x轴进行;对于垂直标题,测量沿y轴进行。- sectionPressed(logicalIndex)¶
- Parameters:
logicalIndex – int
当按下某个部分时,会发出此信号。该部分的逻辑索引由
logicalIndex
指定。- sectionResizeMode(logicalIndex)¶
- Parameters:
logicalIndex – int
- Return type:
返回适用于由给定
logicalIndex
指定的部分的调整大小模式。- sectionResized(logicalIndex, oldSize, newSize)¶
- Parameters:
logicalIndex – int
oldSize – int
newSize – int
当某个部分被调整大小时,会发出此信号。该部分的逻辑编号由
logicalIndex
指定,旧的大小由oldSize
指定,新的大小由newSize
指定。另请参阅
- sectionSize(logicalIndex)¶
- Parameters:
logicalIndex – int
- Return type:
整数
返回给定
logicalIndex
的宽度(或垂直标题的高度)。返回由给定的
logicalIndex
指定的部分内容的大小。另请参阅
- sectionSizeHint(logicalIndex)¶
- Parameters:
logicalIndex – int
- Return type:
整数
返回由
logicalIndex
指定的部分的合适大小提示。Qt::SizeHintRole
另请参阅
sizeHint()
defaultSectionSize()
minimumSectionSize()
maximumSectionSize()
- sectionViewportPosition(logicalIndex)¶
- Parameters:
logicalIndex – int
- Return type:
整数
返回给定
logicalIndex
的视口位置。如果该部分被隐藏,则返回值为未定义。
- sectionsAboutToBeRemoved(parent, logicalFirst, logicalLast)¶
- Parameters:
parent –
QModelIndex
logicalFirst – int
logicalLast – int
当从
parent
中移除部分时,会调用此插槽。logicalFirst
和logicalLast
表示移除部分的位置。如果只移除一个部分,
logicalFirst
和logicalLast
将会相同。- sectionsClickable()¶
- Return type:
布尔
返回 sectionsClickable。
属性
sectionsClickableᅟ
的获取器。- sectionsHidden()¶
- Return type:
布尔
如果标题中的部分已被隐藏,则返回
true
;否则返回 false;另请参阅
- sectionsInserted(parent, logicalFirst, logicalLast)¶
- Parameters:
parent –
QModelIndex
logicalFirst – int
logicalLast – int
当部分插入到
parent
时,会调用此插槽。logicalFirst
和logicalLast
索引表示新部分插入的位置。如果只插入一个部分,
logicalFirst
和logicalLast
将会相同。- sectionsMovable()¶
- Return type:
布尔
返回 sectionsMovable。
另请参阅
属性
sectionsMovableᅟ
的获取器。- sectionsMoved()¶
- Return type:
布尔
如果标题中的部分已被移动,则返回
true
;否则返回 false;另请参阅
- setCascadingSectionResizes(enable)¶
- Parameters:
enable – 布尔值
属性
cascadingSectionResizesᅟ
的设置器。- setDefaultAlignment(alignment)¶
- Parameters:
alignment –
AlignmentFlag
的组合
另请参阅
属性
defaultAlignmentᅟ
的设置器。- setDefaultSectionSize(size)¶
- Parameters:
size – int
另请参阅
属性
defaultSectionSizeᅟ
的设置器。- setFirstSectionMovable(movable)¶
- Parameters:
movable – 布尔值
属性
firstSectionMovableᅟ
的设置器。- setHighlightSections(highlight)¶
- Parameters:
highlight – 布尔值
另请参阅
属性
highlightSectionsᅟ
的设置器。- setMaximumSectionSize(size)¶
- Parameters:
size – int
另请参阅
属性
maximumSectionSizeᅟ
的设置器。- setMinimumSectionSize(size)¶
- Parameters:
size – int
另请参阅
属性
minimumSectionSizeᅟ
的设置器。- setOffset(offset)¶
- Parameters:
offset – int
将标题的偏移量设置为
offset
。- setOffsetToLastSection()¶
设置偏移量以使最后一部分可见。
- setOffsetToSectionPosition(visualIndex)¶
- Parameters:
visualIndex – int
设置偏移量到给定
visualSectionNumber
部分的开始。visualSectionNumber
是不考虑隐藏部分时的实际可见部分。这并不总是与visualIndex()
相同。- setResizeContentsPrecision(precision)¶
- Parameters:
精度 – int
设置当使用
ResizeToContents
时,QHeaderView
应如何精确计算大小。较低的值将提供不太准确但快速的自动调整大小,而较高的值将提供更准确的调整大小,但可能会较慢。数字
precision
指定在计算首选大小时应考虑多少个部分。默认值为1000,这意味着在自动调整大小时,具有自动调整大小的水平列将最多查看1000行进行计算。
特殊值0表示仅查看可见区域。特殊值-1表示查看所有元素。
此值用于
sizeHintForColumn()
、sizeHintForRow()
和sizeHintForColumn()
。重新实现这些函数可能会使此函数无效。另请参阅
resizeContentsPrecision()
setSectionResizeMode()
resizeSections()
sizeHintForColumn()
sizeHintForRow()
sizeHintForColumn()
- setSectionHidden(logicalIndex, hide)¶
- Parameters:
logicalIndex – int
hide – 布尔值
如果
hide
为 true,则由logicalIndex
指定的部分将被隐藏;否则该部分将显示。- setSectionResizeMode(mode)¶
- Parameters:
mode –
ResizeMode
设置标题栏如何根据给定的
mode
调整大小的约束条件。- setSectionResizeMode(logicalIndex, mode)
- Parameters:
logicalIndex – int
mode –
ResizeMode
设置对标题中由
logicalIndex
指定的部分如何调整大小的约束,这些约束由给定的mode
描述。在调用此函数时,逻辑索引应该存在。注意
如果
stretchLastSection
属性设置为true,则此设置将在最后一部分被忽略。这是QTreeView
提供的水平标题的默认设置。- setSectionsClickable(clickable)¶
- Parameters:
可点击 – 布尔值
将
sectionsClickable
设置为clickable
。另请参阅
属性
sectionsClickableᅟ
的设置器。- setSectionsMovable(movable)¶
- Parameters:
movable – 布尔值
将
sectionsMovable
设置为movable
。另请参阅
属性
sectionsMovableᅟ
的设置器。为通过
logicalIndex
指定的部分设置由order
指定的方向的排序指示器,并从显示该指示器的任何其他部分移除排序指示器。logicalIndex
可能为 -1,在这种情况下不会显示排序指示符,模型将返回其自然的未排序顺序。请注意,并非所有模型都支持此操作,甚至可能在这种情况下崩溃。- setSortIndicatorClearable(clearable)¶
- Parameters:
clearable – 布尔值
属性
sortIndicatorClearableᅟ
的设置器。- setSortIndicatorShown(show)¶
- Parameters:
显示 – 布尔值
属性
showSortIndicatorᅟ
的设置器。- setStretchLastSection(stretch)¶
- Parameters:
stretch – 布尔值
另请参阅
属性
stretchLastSectionᅟ
的设置器。- showSection(logicalIndex)¶
- Parameters:
logicalIndex – int
显示由
logicalIndex
指定的部分。当包含排序指示器的部分或指示的顺序发生变化时,会发出此信号。部分的逻辑索引由
logicalIndex
指定,排序顺序由order
指定。另请参阅
- sortIndicatorClearableChanged(clearable)¶
- Parameters:
clearable – 布尔值
属性
sortIndicatorClearableᅟ
的通知信号。返回排序指示器的顺序。如果没有部分具有排序指示器,则此函数的返回值未定义。
- sortIndicatorSection()¶
- Return type:
整数
返回具有排序指示符的章节的逻辑索引。默认情况下,这是第0节。
- stretchLastSection()¶
- Return type:
布尔
属性
stretchLastSectionᅟ
的获取器。- stretchSectionCount()¶
- Return type:
整数
返回设置为调整大小模式拉伸的节数。在视图中,这可以用于查看当视图的几何形状发生变化时,头视图是否需要调整节的大小。
另请参阅
- swapSections(first, second)¶
- Parameters:
first – int
second – int
将视觉索引为
first
的部分与视觉索引为second
的部分交换。另请参阅
- updateSection(logicalIndex)¶
- Parameters:
logicalIndex – int
- visualIndex(logicalIndex)¶
- Parameters:
logicalIndex – int
- Return type:
整数
返回由给定的
logicalIndex
指定的部分的视觉索引位置,否则返回-1。隐藏部分仍然具有有效的视觉索引。
另请参阅
- visualIndexAt(position)¶
- Parameters:
position – int
- Return type:
整数
返回视口中覆盖给定
position
的视觉索引。另请参阅