QHeaderView Class
The QHeaderView class provides a header row or header column for item views. More...
| Header: | #include <QHeaderView> |
| qmake: | QT += widgets |
| Inherits: | QAbstractItemView |
Public Types
| enum | ResizeMode { Interactive, Fixed, Stretch, ResizeToContents, Custom } |
Properties
|
|
Public Functions
| bool | cascadingSectionResizes() const |
| Qt::Alignment | defaultAlignment() const |
| int | defaultSectionSize() const |
| bool | highlightSections() const |
| bool | isFirstSectionMovable() const |
| bool | isSortIndicatorShown() const |
| int | maximumSectionSize() const |
| int | minimumSectionSize() const |
| void | resetDefaultSectionSize() |
| 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 | setSortIndicatorShown(bool show) |
| void | setStretchLastSection(bool stretch) |
| bool | stretchLastSection() const |
Detailed Description
A QHeaderView displays the headers used in item views such as the QTableView and QTreeView classes. It takes the place of Qt3's QHeader class previously used for the same purpose, but uses the Qt's model/view architecture for consistency with the item view classes.
The QHeaderView class is one of the Model/View Classes and is part of Qt's model/view framework.
The header gets the data for each section from the model using the QAbstractItemModel::headerData() function. You can set the data by using QAbstractItemModel::setHeaderData().
Each header has an orientation() and a number of sections, given by the count() function. A section refers to a part of the header - either a row or a column, depending on the orientation.
Sections can be moved and resized using moveSection() and resizeSection(); they can also be hidden and shown with hideSection() and showSection().
Each section of a header is described by a section ID, specified by its section(), and can be located at a particular visualIndex() in the header. A section can have a sort indicator set with setSortIndicator(); this indicates whether the items in the associated item view will be sorted in the order given by the section.
For a horizontal header the section is equivalent to a column in the model, and for a vertical header the section is equivalent to a row in the model.
Moving Header Sections
A header can be fixed in place, or made movable with setSectionsMovable(). It can be made clickable with setSectionsClickable(), and has resizing behavior in accordance with setSectionResizeMode().
Note: Double-clicking on a header to resize a section only applies for visible rows.
A header will emit sectionMoved() if the user moves a section, sectionResized() if the user resizes a section, and sectionClicked() as well as sectionHandleDoubleClicked() in response to mouse clicks. A header will also emit sectionCountChanged().
You can identify a section using the logicalIndex() and logicalIndexAt() functions, or by its index position, using the visualIndex() and visualIndexAt() functions. The visual index will change if a section is moved, but the logical index will not change.
Appearance
QTableWidget and QTableView create default headers. If you want the headers to be visible, you can use setVisible().
Not all ItemDataRoles will have an effect on a QHeaderView. If you need to draw other roles, you can subclass QHeaderView and reimplement paintEvent(). QHeaderView respects the following item data roles, unless they are in conflict with the style (which can happen for styles that follow the desktop theme):
TextAlignmentRole, DisplayRole, FontRole, DecorationRole, ForegroundRole, and BackgroundRole.
Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect.
See also Model/View Programming, QListView, QTableView, and QTreeView.
Member Type Documentation
enum QHeaderView::ResizeMode
The resize mode specifies the behavior of the header sections. It can be set on the entire header view or on individual sections using setSectionResizeMode().
| Constant | Value | Description |
|---|---|---|
QHeaderView::Interactive | 0 | The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. (See also cascadingSectionResizes.) |
QHeaderView::Fixed | 2 | The user cannot resize the section. The section can only be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. |
QHeaderView::Stretch | 1 | QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically. |
QHeaderView::ResizeToContents | 3 | QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically. (This value was introduced in 4.2) |
The following values are obsolete:
| Constant | Value | Description |
|---|---|---|
QHeaderView::Custom | Fixed | Use Fixed instead. |
See also setSectionResizeMode(), stretchLastSection, and minimumSectionSize.
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.
This property was introduced in Qt 4.2.
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
This property was introduced in Qt 4.1.
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.
Setting it to true has no effect unless setSectionsMovable(true) is called as well.
This property was introduced in Qt 5.11.
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
This property was introduced in Qt 5.2.
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 maximum of the global strut or the font metrics size.
This property is honored by all resize modes.
This property was introduced in Qt 4.2.
Access functions:
| int | minimumSectionSize() const |
| void | setMinimumSectionSize(int size) |
See also setSectionResizeMode() and defaultSectionSize.
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().
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().