QHeaderView Class

  • Header: QHeaderView

  • qmake: QT += widgets

  • Inherits: QAbstractItemView

  • Group: QHeaderView is part of model-view

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().

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.

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

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