|  | Home · All Classes · Modules

# QHeaderView Class Reference
[QtGui module]

The QHeaderView class provides a header row or header column for item views. More...

Inherits QAbstractItemView.

### Types

- enum ResizeMode { Interactive, Fixed, Stretch, ResizeToContents, Custom }

### Methods

- __init__ (self, Qt.Orientation, QWidget parent = None)
- bool cascadingSectionResizes (self)
- int count (self)
- currentChanged (self, QModelIndex, QModelIndex)
- dataChanged (self, QModelIndex, QModelIndex)
- Qt.Alignment defaultAlignment (self)
- int defaultSectionSize (self)
- doItemsLayout (self)
- bool event (self, QEvent)
- headerDataChanged (self, Qt.Orientation, int, int)
- int hiddenSectionCount (self)
- hideSection (self, int)
- bool highlightSections (self)
- int horizontalOffset (self)
- QModelIndex indexAt (self, QPoint)
- initialize (self)
- initializeSections (self)
- initializeSections (self, int, int)
- initStyleOption (self, QStyleOptionHeader)
- bool isClickable (self)
- bool isIndexHidden (self, QModelIndex)
- bool isMovable (self)
- bool isSectionHidden (self, int)
- bool isSortIndicatorShown (self)
- int length (self)
- int logicalIndex (self, int)
- int logicalIndexAt (self, int)
- int logicalIndexAt (self, int, int)
- int logicalIndexAt (self, QPoint)
- int minimumSectionSize (self)
- mouseDoubleClickEvent (self, QMouseEvent)
- mouseMoveEvent (self, QMouseEvent)
- mousePressEvent (self, QMouseEvent)
- mouseReleaseEvent (self, QMouseEvent)
- QModelIndex moveCursor (self, QAbstractItemView.CursorAction, Qt.KeyboardModifiers)
- moveSection (self, int, int)
- int offset (self)
- Qt.Orientation orientation (self)
- paintEvent (self, QPaintEvent)
- paintSection (self, QPainter, QRect, int)
- reset (self)
- ResizeMode resizeMode (self, int)
- resizeSection (self, int, int)
- resizeSections (self)
- resizeSections (self, ResizeMode)
- bool restoreState (self, QByteArray)
- rowsInserted (self, QModelIndex, int, int)
- QByteArray saveState (self)
- scrollContentsBy (self, int, int)
- scrollTo (self, QModelIndex, QAbstractItemView.ScrollHint)
- int sectionPosition (self, int)
- sectionsAboutToBeRemoved (self, QModelIndex, int, int)
- bool sectionsHidden (self)
- sectionsInserted (self, QModelIndex, int, int)
- int sectionSize (self, int)
- QSize sectionSizeFromContents (self, int)
- int sectionSizeHint (self, int)
- bool sectionsMoved (self)
- int sectionViewportPosition (self, int)
- setCascadingSectionResizes (self, bool)
- setClickable (self, bool)
- setDefaultAlignment (self, Qt.Alignment)
- setDefaultSectionSize (self, int)
- setHighlightSections (self, bool)
- setMinimumSectionSize (self, int)
- setModel (self, QAbstractItemModel)
- setMovable (self, bool)
- setOffset (self, int)
- setOffsetToLastSection (self)
- setOffsetToSectionPosition (self, int)
- setResizeMode (self, ResizeMode)
- setResizeMode (self, int, ResizeMode)
- setSectionHidden (self, int, bool)
- setSelection (self, QRect, QItemSelectionModel.SelectionFlags)
- setSortIndicator (self, int, Qt.SortOrder)
- setSortIndicatorShown (self, bool)
- setStretchLastSection (self, bool)
- showSection (self, int)
- QSize sizeHint (self)
- Qt.SortOrder sortIndicatorOrder (self)
- int sortIndicatorSection (self)
- bool stretchLastSection (self)
- int stretchSectionCount (self)
- swapSections (self, int, int)
- updateGeometries (self)
- updateSection (self, int)
- int verticalOffset (self)
- bool viewportEvent (self, QEvent)
- int visualIndex (self, int)
- int visualIndexAt (self, int)
- QRect visualRect (self, QModelIndex)
- QRegion visualRegionForSelection (self, QItemSelection)

### Special Methods

- __len__ (self)

### Qt Signals

- void geometriesChanged ()
- void sectionAutoResize (int,QHeaderView::ResizeMode)
- void sectionClicked (int)
- void sectionCountChanged (int,int)
- void sectionDoubleClicked (int)
- void sectionEntered (int)
- void sectionHandleDoubleClicked (int)
- void sectionMoved (int,int,int)
- void sectionPressed (int)
- void sectionResized (int,int,int)
- void sortIndicatorChanged (int,Qt::SortOrder)

## Detailed Description

The QHeaderView class provides a header row or header column for item views.

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 setMovable(). It can be made clickable with setClickable(), and has resizing behavior in accordance with setResizeMode().

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

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: 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.

## Type Documentation

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

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 setResizeMode(), stretchLastSection, and minimumSectionSize.

## Method Documentation

### QHeaderView.__init__ (self, Qt.Orientation, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Creates a new generic header with the given orientation and parent.

### bool QHeaderView.cascadingSectionResizes (self)

### int QHeaderView.count (self)

Returns the number of sections in the header.

See also sectionCountChanged() and length().

### QHeaderView.currentChanged (self, QModelIndex, QModelIndex)

Reimplemented from QAbstractItemView.currentChanged().

### QHeaderView.dataChanged (self, QModelIndex, QModelIndex)

### Qt.Alignment QHeaderView.defaultAlignment (self)

### int QHeaderView.defaultSectionSize (self)

### QHeaderView.doItemsLayout (self)

### bool QHeaderView.event (self, QEvent)

Reimplemented from QObject.event().

### QHeaderView.headerDataChanged (self, Qt.Orientation, int, int)

This method is also a Qt slot with the C++ signature `void headerDataChanged(Qt::Orientation,int,int)`.

Updates the changed header sections with the given orientation, from logicalFirst to logicalLast inclusive.

### int QHeaderView.hiddenSectionCount (self)

Returns the number of sections in the header that has been hidden.

This function was introduced in Qt 4.1.

See also setSectionHidden() and isSectionHidden().

### QHeaderView.hideSection (self, int)

Hides the section specified by logicalIndex.

See also showSection(), isSectionHidden(), hiddenSectionCount(), and setSectionHidden().

### bool QHeaderView.highlightSections (self)

### int QHeaderView.horizontalOffset (self)

Reimplemented from QAbstractItemView.horizontalOffset().

Returns the horizontal offset of the header. This is 0 for vertical headers.

See also offset().

### QModelIndex QHeaderView.indexAt (self, QPoint)

### QHeaderView.initialize (self)

### QHeaderView.initializeSections (self)

### QHeaderView.initializeSections (self, int, int)

### QHeaderView.initStyleOption (self, QStyleOptionHeader)

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

### bool QHeaderView.isClickable (self)

Returns true if the header is clickable; otherwise returns 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.

See also setClickable().

### bool QHeaderView.isIndexHidden (self, QModelIndex)

### bool QHeaderView.isMovable (self)

Returns true if the header can be moved by the user; otherwise returns false.

See also setMovable().

### bool QHeaderView.isSectionHidden (self, int)

Returns true if the section specified by logicalIndex is explicitly hidden from the user; otherwise returns false.

See also hideSection(), showSection(), setSectionHidden(), and hiddenSectionCount().

### bool QHeaderView.isSortIndicatorShown (self)

### int QHeaderView.length (self)

Returns the length along the orientation of the header.

See also sizeHint(), setResizeMode(), and offset().

### int QHeaderView.logicalIndex (self, int)

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 (self, int)

Returns the section that covers the given position in the viewport.

See also visualIndexAt() and isSectionHidden().

### int QHeaderView.logicalIndexAt (self, int, int)

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.

### int QHeaderView.logicalIndexAt (self, QPoint)

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

### QHeaderView.mouseDoubleClickEvent (self, QMouseEvent)

Reimplemented from QWidget.mouseDoubleClickEvent().

### QHeaderView.mouseMoveEvent (self, QMouseEvent)

Reimplemented from QWidget.mouseMoveEvent().

### QHeaderView.mousePressEvent (self, QMouseEvent)

Reimplemented from QWidget.mousePressEvent().

### QHeaderView.mouseReleaseEvent (self, QMouseEvent)

Reimplemented from QWidget.mouseReleaseEvent().

### QModelIndex QHeaderView.moveCursor (self, QAbstractItemView.CursorAction, Qt.KeyboardModifiers)

### QHeaderView.moveSection (self, int, int)

Moves the section at visual index from to occupy visual index to.

See also sectionsMoved().

### int QHeaderView.offset (self)

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

Returns the orientation of the header.

See also Qt.Orientation.

### QHeaderView.paintEvent (self, QPaintEvent)

Reimplemented from QWidget.paintEvent().

### QHeaderView.paintSection (self, QPainter, QRect, int)

Paints the section specified by the given logicalIndex, using the given painter and rect.

Normally, you do not have to call this function.

### QHeaderView.reset (self)

Reimplemented from QAbstractItemView.reset().

### ResizeMode QHeaderView.resizeMode (self, int)

Returns the resize mode that applies to the section specified by the given logicalIndex.

See also setResizeMode().

### QHeaderView.resizeSection (self, int, int)

Resizes the section specified by logicalIndex to size measured in pixels.

See also sectionResized(), resizeMode(), and sectionSize().

### QHeaderView.resizeSections (self)

This method is also a Qt slot with the C++ signature `void resizeSections()`.

Resizes the sections according to the given mode, ignoring the current resize mode.

See also resizeMode() and sectionResized().

### QHeaderView.resizeSections (self, ResizeMode)

Resizes the sections according to their size hints. Normally, you do not have to call this function.

### bool QHeaderView.restoreState (self, QByteArray)

Restores the state of this header view. This function returns `true` if the state was restored; otherwise returns false.

This function was introduced in Qt 4.3.

See also saveState().

### QHeaderView.rowsInserted (self, QModelIndex, int, int)

### QByteArray QHeaderView.saveState (self)

Saves the current state of this header view.

To restore the saved state, pass the return value to restoreState().

This function was introduced in Qt 4.3.

See also restoreState().

### QHeaderView.scrollContentsBy (self, int, int)

### QHeaderView.scrollTo (self, QModelIndex, QAbstractItemView.ScrollHint)

### int QHeaderView.sectionPosition (self, int)

Returns the section position of the given logicalIndex, or -1 if the section is hidden.

See also sectionViewportPosition().

### QHeaderView.sectionsAboutToBeRemoved (self, QModelIndex, int, int)

This method is also a Qt slot with the C++ signature `void sectionsAboutToBeRemoved(const QModelIndex&,int,int)`.

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

Returns true if sections in the header has been hidden; otherwise returns false;

This function was introduced in Qt 4.1.

See also setSectionHidden().

### QHeaderView.sectionsInserted (self, QModelIndex, int, int)

This method is also a Qt slot with the C++ signature `void sectionsInserted(const QModelIndex&,int,int)`.

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.

### int QHeaderView.sectionSize (self, int)

Returns the width (or height for vertical headers) of the given logicalIndex.

See also length(), setResizeMode(), and defaultSectionSize().

### QSize QHeaderView.sectionSizeFromContents (self, int)

Returns the size of the contents of the section specified by the given logicalIndex.

See also defaultSectionSize().

### int QHeaderView.sectionSizeHint (self, int)

Returns a suitable size hint for the section specified by logicalIndex.

See also sizeHint(), defaultSectionSize(), minimumSectionSize(), and Qt.SizeHintRole.

### bool QHeaderView.sectionsMoved (self)

Returns true if sections in the header has been moved; otherwise returns false;

See also moveSection().

### int QHeaderView.sectionViewportPosition (self, int)

Returns the section viewport position of the given logicalIndex.

If the section is hidden, the return value is undefined.

See also sectionPosition() and isSectionHidden().

### QHeaderView.setCascadingSectionResizes (self, bool)

### QHeaderView.setClickable (self, bool)

If clickable is true, the header will respond to single clicks.

See also isClickable(), sectionClicked(), sectionPressed(), and setSortIndicatorShown().

### QHeaderView.setDefaultAlignment (self, Qt.Alignment)

### QHeaderView.setDefaultSectionSize (self, int)

### QHeaderView.setHighlightSections (self, bool)

### QHeaderView.setMinimumSectionSize (self, int)

### QHeaderView.setModel (self, QAbstractItemModel)

Reimplemented from QAbstractItemView.setModel().

### QHeaderView.setMovable (self, bool)

If movable is true, the header may be moved by the user; otherwise it is fixed in place.

See also isMovable() and sectionMoved().

### QHeaderView.setOffset (self, int)

This method is also a Qt slot with the C++ signature `void setOffset(int)`.

Sets the header's offset to offset.

See also offset() and length().

### QHeaderView.setOffsetToLastSection (self)

This method is also a Qt slot with the C++ signature `void setOffsetToLastSection()`.

Sets the offset to make the last section visible.

This function was introduced in Qt 4.2.

See also setOffset(), sectionPosition(), and setOffsetToSectionPosition().

### QHeaderView.setOffsetToSectionPosition (self, int)

This method is also a Qt slot with the C++ signature `void setOffsetToSectionPosition(int)`.

Sets the offset to the start of the section at the given visualIndex.

This function was introduced in Qt 4.2.

See also setOffset() and sectionPosition().

### QHeaderView.setResizeMode (self, ResizeMode)

Sets the constraints on how the header can be resized to those described by the given mode.

See also resizeMode(), length(), sectionResized(), and sectionAutoResize().

### QHeaderView.setResizeMode (self, int, ResizeMode)

This is an overloaded function.

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

### QHeaderView.setSectionHidden (self, int, bool)

If hide is true the section specified by logicalIndex is hidden; otherwise the section is shown.

See also isSectionHidden() and hiddenSectionCount().

### QHeaderView.setSelection (self, QRect, QItemSelectionModel.SelectionFlags)

Reimplemented from QAbstractItemView.setSelection().

Selects the items in the given rect according to the specified flags.

The base class implementation does nothing.

### QHeaderView.setSortIndicator (self, int, Qt.SortOrder)

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

### QHeaderView.setSortIndicatorShown (self, bool)

### QHeaderView.setStretchLastSection (self, bool)

### QHeaderView.showSection (self, int)

Shows the section specified by logicalIndex.

See also hideSection(), isSectionHidden(), hiddenSectionCount(), and setSectionHidden().

### QSize QHeaderView.sizeHint (self)

Reimplemented from QWidget.sizeHint().

Returns a suitable size hint for this header.

See also sectionSizeHint().

### Qt.SortOrder QHeaderView.sortIndicatorOrder (self)

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

Returns the logical index of the section that has a sort indicator. By default this is section 0.

See also setSortIndicator(), sortIndicatorOrder(), and setSortIndicatorShown().

### bool QHeaderView.stretchLastSection (self)

### int QHeaderView.stretchSectionCount (self)

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.

This function was introduced in Qt 4.1.

See also stretchLastSection and resizeMode().

### QHeaderView.swapSections (self, int, int)

Swaps the section at visual index first with the section at visual index second.

This function was introduced in Qt 4.2.

See also moveSection().

### QHeaderView.updateGeometries (self)

### QHeaderView.updateSection (self, int)

This method is also a Qt slot with the C++ signature `void updateSection(int)`.

### int QHeaderView.verticalOffset (self)

Reimplemented from QAbstractItemView.verticalOffset().

Returns the vertical offset of the header. This is 0 for horizontal headers.

See also offset().

### bool QHeaderView.viewportEvent (self, QEvent)

Reimplemented from QAbstractScrollArea.viewportEvent().

### int QHeaderView.visualIndex (self, int)

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 (self, int)

Returns the visual index of the section that covers the given position in the viewport.

See also logicalIndexAt().

### QRect QHeaderView.visualRect (self, QModelIndex)

### QRegion QHeaderView.visualRegionForSelection (self, QItemSelection)

### QHeaderView.__len__ (self)

## Qt Signal Documentation

### void geometriesChanged ()

This is the default overload of this signal.

This signal is emitted when the header's geometries have changed.

This function was introduced in Qt 4.2.

### void sectionAutoResize (int,QHeaderView::ResizeMode)

This is the default overload of this signal.

This signal is emitted when a section is automatically resized. The section's logical index is specified by logicalIndex, and the resize mode by mode.

See also setResizeMode() and stretchLastSection().

### void sectionClicked (int)

This is the default overload of this signal.

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 setClickable() and sectionPressed().

### void sectionCountChanged (int,int)

This is the default overload of this signal.

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

### void sectionDoubleClicked (int)

This is the default overload of this signal.

This signal is emitted when a section is double-clicked. The section's logical index is specified by logicalIndex.

See also setClickable().

### void sectionEntered (int)

This is the default overload of this signal.

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.

This function was introduced in Qt 4.3.

See also setClickable() and sectionPressed().

### void sectionHandleDoubleClicked (int)

This is the default overload of this signal.

This signal is emitted when a section is double-clicked. The section's logical index is specified by logicalIndex.

See also setClickable().

### void sectionMoved (int,int,int)

This is the default overload of this signal.

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

### void sectionPressed (int)

This is the default overload of this signal.

This signal is emitted when a section is pressed. The section's logical index is specified by logicalIndex.

See also setClickable().

### void sectionResized (int,int,int)

This is the default overload of this signal.

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

### void sortIndicatorChanged (int,Qt::SortOrder)

This is the default overload of this signal.

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.

This function was introduced in Qt 4.3.

See also setSortIndicator().