QHeaderView

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

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

A PySide.QtGui.QHeaderView displays the headers used in item views such as the PySide.QtGui.QTableView and PySide.QtGui.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 PySide.QtGui.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 PySide.QtGui.QHeaderView.orientation() and a number of sections, given by the PySide.QtGui.QHeaderView.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 PySide.QtGui.QHeaderView.moveSection() and PySide.QtGui.QHeaderView.resizeSection() ; they can also be hidden and shown with PySide.QtGui.QHeaderView.hideSection() and PySide.QtGui.QHeaderView.showSection() .

Each section of a header is described by a section ID, specified by its section() , and can be located at a particular PySide.QtGui.QHeaderView.visualIndex() in the header. A section can have a sort indicator set with PySide.QtGui.QHeaderView.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 PySide.QtGui.QHeaderView.setMovable() . It can be made clickable with PySide.QtGui.QHeaderView.setClickable() , and has resizing behavior in accordance with PySide.QtGui.QHeaderView.setResizeMode() .

Note

Double-clicking on a header to resize a section only applies for visible rows.

A header will emit PySide.QtGui.QHeaderView.sectionMoved() if the user moves a section, PySide.QtGui.QHeaderView.sectionResized() if the user resizes a section, and PySide.QtGui.QHeaderView.sectionClicked() as well as PySide.QtGui.QHeaderView.sectionHandleDoubleClicked() in response to mouse clicks. A header will also emit PySide.QtGui.QHeaderView.sectionCountChanged() and PySide.QtGui.QHeaderView.sectionAutoResize() .

You can identify a section using the PySide.QtGui.QHeaderView.logicalIndex() and PySide.QtGui.QHeaderView.logicalIndexAt() functions, or by its index position, using the PySide.QtGui.QHeaderView.visualIndex() and PySide.QtGui.QHeaderView.visualIndexAt() functions. The visual index will change if a section is moved, but the logical index will not change.

Appearance

PySide.QtGui.QTableWidget and PySide.QtGui.QTableView create default headers. If you want the headers to be visible, you can use PySide.QtGui.QFrame.setVisible() .

Not all Qt.ItemDataRole s will have an effect on a PySide.QtGui.QHeaderView . If you need to draw other roles, you can subclass PySide.QtGui.QHeaderView and reimplement PySide.QtGui.QHeaderView.paintEvent() . PySide.QtGui.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 PySide.QtGui.QAbstractItemView.setItemDelegate() function will have no effect.

class PySide.QtGui.QHeaderView(orientation[, parent=None])
Parameters:
PySide.QtGui.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 PySide.QtGui.QHeaderView.setResizeMode() .

Constant Description
QHeaderView.Interactive The user can resize the section. The section can also be resized programmatically using PySide.QtGui.QHeaderView.resizeSection() . The section size defaults to PySide.QtGui.QHeaderView.defaultSectionSize() . (See also PySide.QtGui.QHeaderView.cascadingSectionResizes() .)
QHeaderView.Fixed The user cannot resize the section. The section can only be resized programmatically using PySide.QtGui.QHeaderView.resizeSection() . The section size defaults to PySide.QtGui.QHeaderView.defaultSectionSize() .
QHeaderView.Stretch PySide.QtGui.QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically.
QHeaderView.ResizeToContents PySide.QtGui.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 Description
QHeaderView.Custom Use Fixed instead.
PySide.QtGui.QHeaderView.cascadingSectionResizes()
Return type:PySide.QtCore.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.

PySide.QtGui.QHeaderView.count()
Return type:PySide.QtCore.int

Returns the number of sections in the header.

PySide.QtGui.QHeaderView.defaultAlignment()
Return type:PySide.QtCore.Qt.Alignment

This property holds the default alignment of the text in each header section.

PySide.QtGui.QHeaderView.defaultSectionSize()
Return type:PySide.QtCore.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.

PySide.QtGui.QHeaderView.geometriesChanged()
PySide.QtGui.QHeaderView.headerDataChanged(orientation, logicalFirst, logicalLast)
Parameters:
  • orientationPySide.QtCore.Qt.Orientation
  • logicalFirstPySide.QtCore.int
  • logicalLastPySide.QtCore.int
PySide.QtGui.QHeaderView.hiddenSectionCount()
Return type:PySide.QtCore.int

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

PySide.QtGui.QHeaderView.hideSection(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int

Hides the section specified by logicalIndex .

PySide.QtGui.QHeaderView.highlightSections()
Return type:PySide.QtCore.bool

This property holds whether the sections containing selected items are highlighted.

By default, this property is false.

PySide.QtGui.QHeaderView.initStyleOption(option)
Parameters:optionPySide.QtGui.QStyleOptionHeader

Initialize option with the values from this PySide.QtGui.QHeaderView . This method is useful for subclasses when they need a PySide.QtGui.QStyleOptionHeader , but do not want to fill in all the information themselves.

PySide.QtGui.QHeaderView.initialize()
PySide.QtGui.QHeaderView.initializeSections()
PySide.QtGui.QHeaderView.initializeSections(start, end)
Parameters:
  • startPySide.QtCore.int
  • endPySide.QtCore.int
PySide.QtGui.QHeaderView.isClickable()
Return type:PySide.QtCore.bool

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.

PySide.QtGui.QHeaderView.isMovable()
Return type:PySide.QtCore.bool

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

PySide.QtGui.QHeaderView.isSectionHidden(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtCore.bool

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

PySide.QtGui.QHeaderView.isSortIndicatorShown()
Return type:PySide.QtCore.bool

This property holds whether the sort indicator is shown.

By default, this property is false.

PySide.QtGui.QHeaderView.length()
Return type:PySide.QtCore.int

Returns the length along the orientation of the header.

PySide.QtGui.QHeaderView.logicalIndex(visualIndex)
Parameters:visualIndexPySide.QtCore.int
Return type:PySide.QtCore.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.

PySide.QtGui.QHeaderView.logicalIndexAt(x, y)
Parameters:
  • xPySide.QtCore.int
  • yPySide.QtCore.int
Return type:

PySide.QtCore.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.

PySide.QtGui.QHeaderView.logicalIndexAt(position)
Parameters:positionPySide.QtCore.int
Return type:PySide.QtCore.int

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

PySide.QtGui.QHeaderView.logicalIndexAt(pos)
Parameters:posPySide.QtCore.QPoint
Return type:PySide.QtCore.int

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.

PySide.QtGui.QHeaderView.minimumSectionSize()
Return type:PySide.QtCore.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, PySide.QtGui.QHeaderView will use the maximum of the global strut or the font metrics size.

This property is honored by all resize modes .

PySide.QtGui.QHeaderView.moveSection(from, to)
Parameters:
  • fromPySide.QtCore.int
  • toPySide.QtCore.int

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

PySide.QtGui.QHeaderView.offset()
Return type:PySide.QtCore.int

Returns the offset of the header: this is the header’s left-most (or top-most for vertical headers) visible pixel.

PySide.QtGui.QHeaderView.orientation()
Return type:PySide.QtCore.Qt.Orientation

Returns the orientation of the header.

See also

Qt.Orientation

PySide.QtGui.QHeaderView.paintSection(painter, rect, logicalIndex)
Parameters:

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

Normally, you do not have to call this function.

PySide.QtGui.QHeaderView.resizeMode(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtGui.QHeaderView.ResizeMode

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

PySide.QtGui.QHeaderView.resizeSection(logicalIndex, size)
Parameters:
  • logicalIndexPySide.QtCore.int
  • sizePySide.QtCore.int

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

PySide.QtGui.QHeaderView.resizeSections(mode)
Parameters:modePySide.QtGui.QHeaderView.ResizeMode
PySide.QtGui.QHeaderView.resizeSections()

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

PySide.QtGui.QHeaderView.restoreState(state)
Parameters:statePySide.QtCore.QByteArray
Return type:PySide.QtCore.bool

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

PySide.QtGui.QHeaderView.saveState()
Return type:PySide.QtCore.QByteArray

Saves the current state of this header view.

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

PySide.QtGui.QHeaderView.sectionAutoResize(logicalIndex, mode)
Parameters:
PySide.QtGui.QHeaderView.sectionClicked(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
PySide.QtGui.QHeaderView.sectionCountChanged(oldCount, newCount)
Parameters:
  • oldCountPySide.QtCore.int
  • newCountPySide.QtCore.int
PySide.QtGui.QHeaderView.sectionDoubleClicked(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
PySide.QtGui.QHeaderView.sectionEntered(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
PySide.QtGui.QHeaderView.sectionHandleDoubleClicked(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
PySide.QtGui.QHeaderView.sectionMoved(logicalIndex, oldVisualIndex, newVisualIndex)
Parameters:
  • logicalIndexPySide.QtCore.int
  • oldVisualIndexPySide.QtCore.int
  • newVisualIndexPySide.QtCore.int
PySide.QtGui.QHeaderView.sectionPosition(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtCore.int

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.

PySide.QtGui.QHeaderView.sectionPressed(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
PySide.QtGui.QHeaderView.sectionResized(logicalIndex, oldSize, newSize)
Parameters:
  • logicalIndexPySide.QtCore.int
  • oldSizePySide.QtCore.int
  • newSizePySide.QtCore.int
PySide.QtGui.QHeaderView.sectionSize(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtCore.int

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

PySide.QtGui.QHeaderView.sectionSizeFromContents(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtCore.QSize

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

PySide.QtGui.QHeaderView.sectionSizeHint(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtCore.int

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

See also

PySide.QtGui.QHeaderView.sizeHint() PySide.QtGui.QHeaderView.defaultSectionSize() PySide.QtGui.QHeaderView.minimumSectionSize() Qt.SizeHintRole

PySide.QtGui.QHeaderView.sectionViewportPosition(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtCore.int

Returns the section viewport position of the given logicalIndex .

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

PySide.QtGui.QHeaderView.sectionsAboutToBeRemoved(parent, logicalFirst, logicalLast)
Parameters:

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.

PySide.QtGui.QHeaderView.sectionsHidden()
Return type:PySide.QtCore.bool

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

PySide.QtGui.QHeaderView.sectionsInserted(parent, logicalFirst, logicalLast)
Parameters:

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.

PySide.QtGui.QHeaderView.sectionsMoved()
Return type:PySide.QtCore.bool

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

PySide.QtGui.QHeaderView.setCascadingSectionResizes(enable)
Parameters:enablePySide.QtCore.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.

PySide.QtGui.QHeaderView.setClickable(clickable)
Parameters:clickablePySide.QtCore.bool

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

PySide.QtGui.QHeaderView.setDefaultAlignment(alignment)
Parameters:alignmentPySide.QtCore.Qt.Alignment

This property holds the default alignment of the text in each header section.

PySide.QtGui.QHeaderView.setDefaultSectionSize(size)
Parameters:sizePySide.QtCore.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.

PySide.QtGui.QHeaderView.setHighlightSections(highlight)
Parameters:highlightPySide.QtCore.bool

This property holds whether the sections containing selected items are highlighted.

By default, this property is false.

PySide.QtGui.QHeaderView.setMinimumSectionSize(size)
Parameters:sizePySide.QtCore.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, PySide.QtGui.QHeaderView will use the maximum of the global strut or the font metrics size.

This property is honored by all resize modes .

PySide.QtGui.QHeaderView.setMovable(movable)
Parameters:movablePySide.QtCore.bool

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

PySide.QtGui.QHeaderView.setOffset(offset)
Parameters:offsetPySide.QtCore.int

Sets the header’s offset to offset .

PySide.QtGui.QHeaderView.setOffsetToLastSection()

Sets the offset to make the last section visible.

PySide.QtGui.QHeaderView.setOffsetToSectionPosition(visualIndex)
Parameters:visualIndexPySide.QtCore.int

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

PySide.QtGui.QHeaderView.setResizeMode(logicalIndex, mode)
Parameters:

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 PySide.QtGui.QHeaderView.stretchLastSection() property is set to true. This is the default for the horizontal headers provided by PySide.QtGui.QTreeView .

PySide.QtGui.QHeaderView.setResizeMode(mode)
Parameters:modePySide.QtGui.QHeaderView.ResizeMode

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

PySide.QtGui.QHeaderView.setSectionHidden(logicalIndex, hide)
Parameters:
  • logicalIndexPySide.QtCore.int
  • hidePySide.QtCore.bool

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

PySide.QtGui.QHeaderView.setSortIndicator(logicalIndex, order)
Parameters:
  • logicalIndexPySide.QtCore.int
  • orderPySide.QtCore.Qt.SortOrder
PySide.QtGui.QHeaderView.setSortIndicatorShown(show)
Parameters:showPySide.QtCore.bool

This property holds whether the sort indicator is shown.

By default, this property is false.

PySide.QtGui.QHeaderView.setStretchLastSection(stretch)
Parameters:stretchPySide.QtCore.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 PySide.QtGui.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.

PySide.QtGui.QHeaderView.showSection(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int

Shows the section specified by logicalIndex .

PySide.QtGui.QHeaderView.sortIndicatorChanged(logicalIndex, order)
Parameters:
  • logicalIndexPySide.QtCore.int
  • orderPySide.QtCore.Qt.SortOrder
PySide.QtGui.QHeaderView.sortIndicatorOrder()
Return type:PySide.QtCore.Qt.SortOrder

Returns the order for the sort indicator. If no section has a sort indicator the return value of this function is undefined.

PySide.QtGui.QHeaderView.sortIndicatorSection()
Return type:PySide.QtCore.int

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

PySide.QtGui.QHeaderView.stretchLastSection()
Return type:PySide.QtCore.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 PySide.QtGui.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.

PySide.QtGui.QHeaderView.stretchSectionCount()
Return type:PySide.QtCore.int

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.

PySide.QtGui.QHeaderView.swapSections(first, second)
Parameters:
  • firstPySide.QtCore.int
  • secondPySide.QtCore.int

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

PySide.QtGui.QHeaderView.updateSection(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int

Updates the section specified by the given logicalIndex .

PySide.QtGui.QHeaderView.visualIndex(logicalIndex)
Parameters:logicalIndexPySide.QtCore.int
Return type:PySide.QtCore.int

Returns the visual index position of the section specified by the given logicalIndex , or -1 otherwise.

Hidden sections still have valid visual indexes.

PySide.QtGui.QHeaderView.visualIndexAt(position)
Parameters:positionPySide.QtCore.int
Return type:PySide.QtCore.int

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