QHeaderView Class
The QHeaderView class provides the row or column headers of a table. More…
| Header | #include <QHeaderView> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QWidget |
List of all members, including inherited members
Public Functions
| QHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) | |
| ~QHeaderView() | |
| Qt::Orientation | orientation() const |
| int | count() const |
| int | length() const |
| int | offset() const |
| void | setOffset(int offset) |
| int | defaultSectionSize() const |
| void | setDefaultSectionSize(int size) |
| int | minimumSectionSize() const |
| void | setMinimumSectionSize(int size) |
| int | sectionSize(int logicalIndex) const |
| void | resizeSection(int logicalIndex, int size) |
| int | sectionPosition(int logicalIndex) const |
| int | logicalIndexAt(int position) const |
| bool | stretchLastSection() const |
| void | setStretchLastSection(bool stretch) |
| bool | sectionsClickable() const |
| void | setSectionsClickable(bool clickable) |
| bool | isSortIndicatorShown() const |
| void | setSortIndicatorShown(bool show) |
| void | setSortIndicator(int logicalIndex, Qt::SortOrder order) |
| int | sortIndicatorSection() const |
| Qt::SortOrder | sortIndicatorOrder() const |
Reimplemented Public Functions
Signals
| void | sectionClicked(int logicalIndex) |
| void | sortIndicatorChanged(int logicalIndex, Qt::SortOrder order) |
| void | sectionResized(int logicalIndex, int oldSize, int newSize) |
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | mouseMoveEvent(QMouseEvent *event) |
| void | mouseReleaseEvent(QMouseEvent *event) |
| void | resizeEvent(QResizeEvent *event) |
Detailed Description
A header is a row of sections, each with a label and a size. It is horizontal, for the columns of a table, or vertical, for its rows. The user drags the edge between two sections to resize the one before it, and clicks a section if the header has clickable sections.
A QTableWidget has both headers, and gives them their sections and labels: use QTableWidget::horizontalHeader() and QTableWidget::verticalHeader() to reach them. The sizes of the sections of the headers are the widths of the columns and the heights of the rows.
table->horizontalHeader()->setStretchLastSection(true);
table->horizontalHeader()->resizeSection(0, 160);
connect(table->horizontalHeader(), &QHeaderView::sectionClicked, [](int column) { qDebug() << column; });
The headers of a table: the column labels, with the last section stretched, and the row labels.

See also QTableWidget.
Differences from Qt
There is no model: the sections and their labels come from the table that owns the header, so the count and the labels cannot be set on a header on its own. Sections cannot be moved, hidden, sorted or selected, and there are no resize modes (setSectionResizeMode()); every section but the last can be resized by the user, and the last one can be stretched. There are no logical and visual indexes: they are the same.
Member Function Documentation
QHeaderView::QHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
Constructs an empty header with the given orientation and parent. The sections of a header are made by the table that has it.
QHeaderView::~QHeaderView()
Destroys the header.
int QHeaderView::count() const
Returns the number of sections.
int QHeaderView::defaultSectionSize() const
Returns the size of the sections that are made from now on. It is 100 for a horizontal header and 30 for a vertical one, as the style says.
See also setDefaultSectionSize().
bool QHeaderView::isSortIndicatorShown() const
Returns whether the header shows which section its view is sorted by. The default is false.
See also setSortIndicatorShown().
int QHeaderView::length() const
Returns the total size of all sections.
See also sectionSize().
int QHeaderView::logicalIndexAt(int position) const
Returns the section at position, which is a position in the widget, along the header: the offset is taken into account. It is -1 if there is no section there.
See also sectionPosition().
int QHeaderView::minimumSectionSize() const
Returns the smallest size that a section can be resized to, 20 by default.
See also setMinimumSectionSize().
void QHeaderView::mouseMoveEvent(QMouseEvent *event)
Reimplements QWidget::mouseMoveEvent().
void QHeaderView::mousePressEvent(QMouseEvent *event)
Reimplements QWidget::mousePressEvent().
void QHeaderView::mouseReleaseEvent(QMouseEvent *event)
Reimplements QWidget::mouseReleaseEvent().
int QHeaderView::offset() const
Returns how far the header is scrolled, in pixels: the part of the first sections that is out of view.
See also setOffset().
Qt::Orientation QHeaderView::orientation() const
Returns whether the header is horizontal (the headers of the columns) or vertical (the headers of the rows).
void QHeaderView::paintEvent(QPaintEvent *event)
Reimplements QWidget::paintEvent().
void QHeaderView::resizeEvent(QResizeEvent *event)
Reimplements QWidget::resizeEvent().
void QHeaderView::resizeSection(int logicalIndex, int size)
Resizes the section logicalIndex to size pixels, or to minimumSectionSize() if that is more. Emits sectionResized().
See also sectionSize().
void QHeaderView::sectionClicked(int logicalIndex)
This signal is emitted when the user clicks the section logicalIndex, if the sections are clickable.
int QHeaderView::sectionPosition(int logicalIndex) const
Returns the position of the start of the section logicalIndex, from the start of the header and not counting offset(). It is the size of the sections before it.
See also logicalIndexAt() and sectionSize().
void QHeaderView::sectionResized(int logicalIndex, int oldSize, int newSize)
This signal is emitted when the section logicalIndex changes its size from oldSize to newSize, whether the user dragged its edge or resizeSection() was called.
bool QHeaderView::sectionsClickable() const
Returns whether a click on a section is reported with sectionClicked(), and the section is drawn pressed while the button is down. The default is false for a header, and a table turns it on for its own.
See also setSectionsClickable().
int QHeaderView::sectionSize(int logicalIndex) const
Returns the size of the section logicalIndex: its width in a horizontal header, its height in a vertical one. A last section that is stretched can be larger than it was set. The size of a section that does not exist is 0.
See also resizeSection() and stretchLastSection().
void QHeaderView::setDefaultSectionSize(int size)
Sets the size of the sections that are made from now on to size. Sections that exist keep their size.
See also defaultSectionSize().
void QHeaderView::setMinimumSectionSize(int size)
Sets the smallest size that a section can be resized to to size.
See also minimumSectionSize().
void QHeaderView::setOffset(int offset)
Scrolls the header to offset pixels. A table keeps it equal to the scroll position of its contents.
See also offset().
void QHeaderView::setSectionsClickable(bool clickable)
Sets whether the sections can be clicked to clickable.
See also sectionsClickable().
void QHeaderView::setSortIndicator(int logicalIndex, Qt::SortOrder order)
Makes the section logicalIndex the one that shows the sort indicator, for the order order, and emits sortIndicatorChanged() if that is a change. A logicalIndex of -1 means that the view is not sorted.
See also sortIndicatorSection() and sortIndicatorOrder().
void QHeaderView::setSortIndicatorShown(bool show)
Sets whether the section that the view is sorted by has an arrow for the sort order to show. With the arrow shown and sectionsClickable() on, a click on a section moves the arrow to it, or turns it round, and sortIndicatorChanged() tells.
See also isSortIndicatorShown() and setSortIndicator().
void QHeaderView::setStretchLastSection(bool stretch)
Sets whether the last section grows to fill the header to stretch.
See also stretchLastSection().
QSize QHeaderView::sizeHint() const
Reimplements QWidget::sizeHint().
void QHeaderView::sortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
This signal is emitted when the section that shows the sort indicator, or the order that it shows, changes: logicalIndex is the section and order the order. A view sorts its model by it.
See also setSortIndicator().
Qt::SortOrder QHeaderView::sortIndicatorOrder() const
Returns the order that the sort indicator shows.
See also setSortIndicator().
int QHeaderView::sortIndicatorSection() const
Returns the section that shows the sort indicator, or -1 if none does.
See also setSortIndicator().
bool QHeaderView::stretchLastSection() const
Returns whether the last section takes the room that the others leave. The default is false.
See also setStretchLastSection().