QHeaderView Class | Qt Widgets (original) (raw)
The QHeaderView class provides a header row or header column for item views. More...
Property Documentation
cascadingSectionResizes : bool
This property holds whether interactive resizing will be cascaded to the following sections once the section being resized by the user has reached its minimum size
This property only affects sections that have Interactive as their resize mode.
The default value is false.
Access functions:
bool | cascadingSectionResizes() const |
---|---|
void | setCascadingSectionResizes(bool enable) |
See also setSectionResizeMode().
defaultAlignment : Qt::Alignment
This property holds the default alignment of the text in each header section
Access functions:
Qt::Alignment | defaultAlignment() const |
---|---|
void | setDefaultAlignment(Qt::Alignment alignment) |
defaultSectionSize : int
This property holds the default size of the header sections before resizing.
This property only affects sections that have Interactive or Fixed as their resize mode.
By default, the value of this property is style dependent. Thus, when the style changes, this property updates from it. Calling setDefaultSectionSize() stops the updates, calling resetDefaultSectionSize() will restore default behavior.
Access functions:
int | defaultSectionSize() const |
---|---|
void | setDefaultSectionSize(int size) |
void | resetDefaultSectionSize() |
See also setSectionResizeMode() and minimumSectionSize.
firstSectionMovable : bool
This property holds whether the first column can be moved by the user
This property controls whether the first column can be moved by the user. In a QTreeView, the first column holds the tree structure and is therefore non-movable by default, even after setSectionsMovable(true).
It can be made movable again, for instance in the case of flat lists without a tree structure, by calling this method. In such a scenario, it is recommended to call QTreeView::setRootIsDecorated(false) as well.
treeView->setRootIsDecorated(false); treeView->header()->setFirstSectionMovable(true);
Setting it to true has no effect unless setSectionsMovable(true) is called as well.
Access functions:
bool | isFirstSectionMovable() const |
---|---|
void | setFirstSectionMovable(bool movable) |
See also setSectionsMovable().
highlightSections : bool
This property holds whether the sections containing selected items are highlighted
By default, this property is false
.
Access functions:
bool | highlightSections() const |
---|---|
void | setHighlightSections(bool highlight) |
maximumSectionSize : int
This property holds the maximum size of the header sections.
The maximum section size is the largest section size allowed. The default value for this property is 1048575, which is also the largest possible size for a section. Setting maximum to -1 will reset the value to the largest section size.
With exception of stretch this property is honored by all resize modes
Access functions:
int | maximumSectionSize() const |
---|---|
void | setMaximumSectionSize(int size) |
See also setSectionResizeMode() and defaultSectionSize.
minimumSectionSize : int
This property holds the minimum size of the header sections.
The minimum section size is the smallest section size allowed. If the minimum section size is set to -1, QHeaderView will use the font metrics size.
This property is honored by all resize modes.
Access functions:
int | minimumSectionSize() const |
---|---|
void | setMinimumSectionSize(int size) |
See also setSectionResizeMode() and defaultSectionSize.
sectionsClickable : bool
Holds true
if the header is clickable; otherwise false
. A clickable header could be set up to allow the user to change the representation of the data in the view related to the header.
Access functions:
See also sectionPressed() and setSortIndicatorShown().
sectionsMovable : bool
If sectionsMovable is true, the header sections may be moved by the user; otherwise they are fixed in place.
When used in combination with QTreeView, the first column is not movable (since it contains the tree structure), by default. You can make it movable with setFirstSectionMovable(true).
Access functions:
See also sectionMoved() and setFirstSectionMovable().
showSortIndicator : bool
This property holds whether the sort indicator is shown
By default, this property is false
.
Access functions:
bool | isSortIndicatorShown() const |
---|---|
void | setSortIndicatorShown(bool show) |
See also setSectionsClickable().
[since 6.1]
sortIndicatorClearable : bool
This property holds whether the sort indicator can be cleared by clicking on a section multiple times
This property controls whether the user is able to remove the sorting indicator on a given section by clicking on the section multiple times. Normally, clicking on a section will simply change the sorting order for that section. By setting this property to true, the sorting indicator will be cleared after alternating to ascending and descending; this will typically restore the original sorting of a model.
Setting this property to true has no effect unless sectionsClickable() is also true (which is the default for certain views, for instance QTableView, or is automatically set when making a view sortable, for instance by calling QTreeView::setSortingEnabled).
This property was introduced in Qt 6.1.
Access functions:
bool | isSortIndicatorClearable() const |
---|---|
void | setSortIndicatorClearable(bool clearable) |
Notifier signal:
void | sortIndicatorClearableChanged(bool clearable) |
---|
stretchLastSection : bool
This property holds whether the last visible section in the header takes up all the available space
The default value is false.
Note: The horizontal headers provided by QTreeView are configured with this 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.
Access functions:
bool | stretchLastSection() const |
---|---|
void | setStretchLastSection(bool stretch) |
See also setSectionResizeMode().
Member Function Documentation
[explicit]
QHeaderView::QHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
Creates a new generic header with the given orientation and parent.
[virtual noexcept]
QHeaderView::~QHeaderView()
Destroys the header.
int QHeaderView::count() const
Returns the number of sections in the header.
See also sectionCountChanged() and length().
[override virtual protected]
void QHeaderView::currentChanged(const QModelIndex ¤t, const QModelIndex &old)
Reimplements: QAbstractItemView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous).
[override virtual protected]
bool QHeaderView::event(QEvent *e)
Reimplements: QAbstractItemView::event(QEvent *event).
[signal]
void QHeaderView::geometriesChanged()
This signal is emitted when the header's geometries have changed.
[slot]
void QHeaderView::headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast)
Updates the changed header sections with the given orientation, from logicalFirst to logicalLast inclusive.
int QHeaderView::hiddenSectionCount() const
Returns the number of sections in the header that has been hidden.
See also setSectionHidden() and isSectionHidden().
void QHeaderView::hideSection(int logicalIndex)
Hides the section specified by logicalIndex.
See also showSection(), isSectionHidden(), hiddenSectionCount(), and setSectionHidden().
[override virtual protected]
int QHeaderView::horizontalOffset() const
Reimplements: QAbstractItemView::horizontalOffset() const.
Returns the horizontal offset of the header. This is 0 for vertical headers.
See also offset().
[virtual protected]
void QHeaderView::initStyleOption(QStyleOptionHeader *option) const
Initialize option with the values from this QHeaderView. This method is useful for subclasses when they need a QStyleOptionHeader, but do not want to fill in all the information themselves.
See also QStyleOption::initFrom() and initStyleOptionForIndex().
[virtual protected, since 6.0]
void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const
Initializes the style option from the specified logicalIndex. This function is called by the default implementation of paintSection after initStyleOption has been called.
This function was introduced in Qt 6.0.
See also paintSection() and initStyleOption().
bool QHeaderView::isSectionHidden(int logicalIndex) const
Returns true
if the section specified by logicalIndex is explicitly hidden from the user; otherwise returns false
.
See also hideSection(), showSection(), setSectionHidden(), and hiddenSectionCount().
int QHeaderView::length() const
Returns the length along the orientation of the header.
See also sizeHint(), setSectionResizeMode(), and offset().
int QHeaderView::logicalIndex(int visualIndex) const
Returns the logicalIndex for the section at the given visualIndex position, or -1 if visualIndex < 0 or visualIndex >= QHeaderView::count().
Note that the visualIndex is not affected by hidden sections.
See also visualIndex() and sectionPosition().
int QHeaderView::logicalIndexAt(const QPoint &pos) const
Returns the logical index of the section at the position given in pos. If the header is horizontal the x-coordinate will be used, otherwise the y-coordinate will be used to find the logical index.
See also sectionPosition().
int QHeaderView::logicalIndexAt(int position) const
Returns the section that covers the given position in the viewport.
See also visualIndexAt() and isSectionHidden().
int QHeaderView::logicalIndexAt(int x, int y) const
Returns the logical index of the section at the given coordinate. If the header is horizontal x will be used, otherwise y will be used to find the logical index.
[override virtual protected]
void QHeaderView::mouseDoubleClickEvent(QMouseEvent *e)
Reimplements: QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event).
[override virtual protected]
void QHeaderView::mouseMoveEvent(QMouseEvent *e)
Reimplements: QAbstractItemView::mouseMoveEvent(QMouseEvent *event).
[override virtual protected]
void QHeaderView::mousePressEvent(QMouseEvent *e)
Reimplements: QAbstractItemView::mousePressEvent(QMouseEvent *event).
[override virtual protected]
void QHeaderView::mouseReleaseEvent(QMouseEvent *e)
Reimplements: QAbstractItemView::mouseReleaseEvent(QMouseEvent *event).
void QHeaderView::moveSection(int from, int to)
Moves the section at visual index from to occupy visual index to.
See also sectionsMoved().
int QHeaderView::offset() const
Returns the offset of the header: this is the header's left-most (or top-most for vertical headers) visible pixel.
See also setOffset().
Qt::Orientation QHeaderView::orientation() const
Returns the orientation of the header.
See also Qt::Orientation.
[override virtual protected]
void QHeaderView::paintEvent(QPaintEvent *e)
Reimplements: QAbstractScrollArea::paintEvent(QPaintEvent *event).
[virtual protected]
void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
Paints the section specified by the given logicalIndex, using the given painter and rect.
Normally, you do not have to call this function.
[override virtual]
void QHeaderView::reset()
Reimplements: QAbstractItemView::reset().
int QHeaderView::resizeContentsPrecision() const
Returns how precise QHeaderView will calculate on ResizeToContents.
See also setResizeContentsPrecision() and setSectionResizeMode().
void QHeaderView::resizeSection(int logicalIndex, int size)
Resizes the section specified by logicalIndex to size measured in pixels. The size parameter must be a value larger or equal to zero. A size equal to zero is however not recommended. In that situation hideSection should be used instead.
See also sectionResized(), sectionSize(), and hideSection().
[protected slot]
void QHeaderView::resizeSections()
Resizes the sections according to their size hints. Normally, you do not have to call this function.
void QHeaderView::resizeSections(QHeaderView::ResizeMode mode)
Resizes the sections according to the given mode, ignoring the current resize mode.
See also sectionResized().
bool QHeaderView::restoreState(const QByteArray &state)
Restores the state of this header view. This function returns true
if the state was restored; otherwise returns false.
See also saveState().
QByteArray QHeaderView::saveState() const
Saves the current state of this header view.
To restore the saved state, pass the return value to restoreState().
See also restoreState().
[signal]
void QHeaderView::sectionClicked(int logicalIndex)
This signal is emitted when a section is clicked. The section's logical index is specified by logicalIndex.
Note that the sectionPressed signal will also be emitted.
See also setSectionsClickable() and sectionPressed().
[signal]
void QHeaderView::sectionCountChanged(int oldCount, int newCount)
This signal is emitted when the number of sections changes, i.e., when sections are added or deleted. The original count is specified by oldCount, and the new count by newCount.
See also count(), length(), and headerDataChanged().
[signal]
void QHeaderView::sectionDoubleClicked(int logicalIndex)
This signal is emitted when a section is double-clicked. The section's logical index is specified by logicalIndex.
See also setSectionsClickable().
[signal]
void QHeaderView::sectionEntered(int logicalIndex)
This signal is emitted when the cursor moves over the section and the left mouse button is pressed. The section's logical index is specified by logicalIndex.
See also setSectionsClickable() and sectionPressed().
[signal]
void QHeaderView::sectionHandleDoubleClicked(int logicalIndex)
This signal is emitted when a section is double-clicked. The section's logical index is specified by logicalIndex.
See also setSectionsClickable().
[signal]
void QHeaderView::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
This signal is emitted when a section is moved. The section's logical index is specified by logicalIndex, the old index by oldVisualIndex, and the new index position by newVisualIndex.
See also moveSection().
int QHeaderView::sectionPosition(int logicalIndex) const
Returns the section position of the given logicalIndex, or -1 if the section is hidden. The position is measured in pixels from the first visible item's top-left corner to the top-left corner of the item with logicalIndex. The measurement is along the x-axis for horizontal headers and along the y-axis for vertical headers.
See also sectionViewportPosition().
[signal]
void QHeaderView::sectionPressed(int logicalIndex)
This signal is emitted when a section is pressed. The section's logical index is specified by logicalIndex.
See also setSectionsClickable().
QHeaderView::ResizeMode QHeaderView::sectionResizeMode(int logicalIndex) const
Returns the resize mode that applies to the section specified by the given logicalIndex.
See also setSectionResizeMode().
[signal]
void QHeaderView::sectionResized(int logicalIndex, int oldSize, int newSize)
This signal is emitted when a section is resized. The section's logical number is specified by logicalIndex, the old size by oldSize, and the new size by newSize.
See also resizeSection().
int QHeaderView::sectionSize(int logicalIndex) const
Returns the width (or height for vertical headers) of the given logicalIndex.
See also length(), setSectionResizeMode(), and defaultSectionSize().
[virtual protected]
QSize QHeaderView::sectionSizeFromContents(int logicalIndex) const
Returns the size of the contents of the section specified by the given logicalIndex.
See also defaultSectionSize().
int QHeaderView::sectionSizeHint(int logicalIndex) const
Returns a suitable size hint for the section specified by logicalIndex.
See also sizeHint(), defaultSectionSize(), minimumSectionSize(), and maximumSectionSize().
int QHeaderView::sectionViewportPosition(int logicalIndex) const
Returns the section viewport position of the given logicalIndex.
If the section is hidden, the return value is undefined.
See also sectionPosition() and isSectionHidden().
[protected slot]
void QHeaderView::sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast)
This slot is called when sections are removed from the parent. logicalFirst and logicalLast signify where the sections were removed.
If only one section is removed, logicalFirst and logicalLast will be the same.
bool QHeaderView::sectionsClickable() const
Returns sectionsClickable.
Note: Getter function for property sectionsClickable.
See also setSectionsClickable().
bool QHeaderView::sectionsHidden() const
Returns true
if sections in the header has been hidden; otherwise returns false;
See also setSectionHidden().
[protected slot]
void QHeaderView::sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast)
This slot is called when sections are inserted into the parent. logicalFirst and logicalLast indices signify where the new sections were inserted.
If only one section is inserted, logicalFirst and logicalLast will be the same.
bool QHeaderView::sectionsMovable() const
Returns sectionsMovable.
Note: Getter function for property sectionsMovable.
See also setSectionsMovable().
bool QHeaderView::sectionsMoved() const
Returns true
if sections in the header has been moved; otherwise returns false;
See also moveSection().
[override virtual]
void QHeaderView::setModel(QAbstractItemModel *model)
Reimplements: QAbstractItemView::setModel(QAbstractItemModel *model).
[slot]
void QHeaderView::setOffset(int offset)
Sets the header's offset to offset.
See also offset() and length().
[slot]
void QHeaderView::setOffsetToLastSection()
Sets the offset to make the last section visible.
See also setOffset(), sectionPosition(), and setOffsetToSectionPosition().
[slot]
void QHeaderView::setOffsetToSectionPosition(int visualSectionNumber)
Sets the offset to the start of the section at the given visualSectionNumber. visualSectionNumber is the actual visible section when hiddenSections are not considered. That is not always the same as visualIndex().
See also setOffset() and sectionPosition().
void QHeaderView::setResizeContentsPrecision(int precision)
Sets how precise QHeaderView should calculate the size when ResizeToContents is used. A low value will provide a less accurate but fast auto resize while a higher value will provide a more accurate resize that however can be slow.
The number precision specifies how many sections that should be consider when calculating the preferred size.
The default value is 1000 meaning that a horizontal column with auto-resize will look at maximum 1000 rows on calculating when doing an auto resize.
Special value 0 means that it will look at only the visible area. Special value -1 will imply looking at all elements.
This value is used in QTableView::sizeHintForColumn(), QTableView::sizeHintForRow() and QTreeView::sizeHintForColumn(). Reimplementing these functions can make this function not having an effect.
See also resizeContentsPrecision(), setSectionResizeMode(), resizeSections(), QTableView::sizeHintForColumn(), QTableView::sizeHintForRow(), and QTreeView::sizeHintForColumn().
void QHeaderView::setSectionHidden(int logicalIndex, bool hide)
If hide is true the section specified by logicalIndex is hidden; otherwise the section is shown.
See also isSectionHidden() and hiddenSectionCount().
void QHeaderView::setSectionResizeMode(QHeaderView::ResizeMode mode)
Sets the constraints on how the header can be resized to those described by the given mode.
See also sectionResizeMode(), length(), and sectionResized().
void QHeaderView::setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)
Sets the constraints on how the section specified by logicalIndex in the header can be resized to those described by the given mode. The logical index should exist at the time this function is called.
Note: This setting will be ignored for the last section if the stretchLastSection property is set to true. This is the default for the horizontal headers provided by QTreeView.
See also setStretchLastSection() and resizeContentsPrecision().
void QHeaderView::setSectionsClickable(bool clickable)
Set sectionsClickable to clickable.
See also sectionsClickable().
void QHeaderView::setSectionsMovable(bool movable)
Sets sectionsMovable to movable.
See also sectionsMovable().
[override virtual protected]
void QHeaderView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)
Reimplements: QAbstractItemView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags).
Selects the items in the given rect according to the specified flags.
The base class implementation does nothing.
void QHeaderView::setSortIndicator(int logicalIndex, Qt::SortOrder order)
Sets the sort indicator for the section specified by the given logicalIndex in the direction specified by order, and removes the sort indicator from any other section that was showing it.
logicalIndex 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.
See also sortIndicatorSection() and sortIndicatorOrder().
[override virtual]
void QHeaderView::setVisible(bool v)
Reimplements an access function for property: QWidget::visible.
void QHeaderView::showSection(int logicalIndex)
Shows the section specified by logicalIndex.
See also hideSection(), isSectionHidden(), hiddenSectionCount(), and setSectionHidden().
[override virtual]
QSize QHeaderView::sizeHint() const
Reimplements: QAbstractScrollArea::sizeHint() const.
Returns a suitable size hint for this header.
See also sectionSizeHint().
[signal]
void QHeaderView::sortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
This signal is emitted when the section containing the sort indicator or the order indicated is changed. The section's logical index is specified by logicalIndex and the sort order is specified by order.
See also setSortIndicator().
Qt::SortOrder QHeaderView::sortIndicatorOrder() const
Returns the order for the sort indicator. If no section has a sort indicator the return value of this function is undefined.
See also setSortIndicator() and sortIndicatorSection().
int QHeaderView::sortIndicatorSection() const
Returns the logical index of the section that has a sort indicator. By default this is section 0.
See also setSortIndicator(), sortIndicatorOrder(), and setSortIndicatorShown().
int QHeaderView::stretchSectionCount() const
Returns the number of sections that are set to resize mode stretch. In views, this can be used to see if the headerview needs to resize the sections when the view's geometry changes.
See also stretchLastSection.
void QHeaderView::swapSections(int first, int second)
Swaps the section at visual index first with the section at visual index second.
See also moveSection().
[override virtual protected]
int QHeaderView::verticalOffset() const
Reimplements: QAbstractItemView::verticalOffset() const.
Returns the vertical offset of the header. This is 0 for horizontal headers.
See also offset().
[override virtual protected]
bool QHeaderView::viewportEvent(QEvent *e)
Reimplements: QAbstractItemView::viewportEvent(QEvent *event).
int QHeaderView::visualIndex(int logicalIndex) const
Returns the visual index position of the section specified by the given logicalIndex, or -1 otherwise.
Hidden sections still have valid visual indexes.
See also logicalIndex().
int QHeaderView::visualIndexAt(int position) const
Returns the visual index of the section that covers the given position in the viewport.
See also logicalIndexAt().