Styling Menus and Item Views

The style elements, states, and options of menus, menu bars, item view headers, tree branch indicators, and item view items.

This page is part of the Widget Style Reference . For an explanation of the element trees, screenshots, and tables, see that page. The common state flags and members that every widget sets are listed in How a Style Draws a Widget .

Item view headers

QHeaderView draws each header section with CE_Header and a QStyleOptionHeaderV2 . QCommonStyle splits it into the section background, the label, and the sort indicator:

  • CE_Header, once per section (PM_HeaderMargin)

    • CE_HeaderSection

    • CE_HeaderLabel (SE_HeaderLabel)

    • PE_IndicatorHeaderArrow (SE_HeaderArrow, PM_HeaderMarkSize), for the sorted section

  • CE_HeaderEmptyArea, for the space after the last section

The header view sizes its sections with CT_HeaderSection and uses PM_HeaderGripMargin for the area in which the user can drag a section boundary. QTableView draws the button in the upper-left corner, where the two headers meet, as a CE_Header. The hint SH_Header_ArrowAlignment places the sort indicator.

../_images/header.webp

The header sets these state flags on a section:

State

Set when

State_Raised

Always. The header sets it for every section.

State_Horizontal

This is the horizontal header above the view.

State_Sunken

The section is pressed, or the section is selected and highlightSections() is on.

State_On

An item in the section is selected and highlightSections() is on.

State_MouseOver

The mouse is over the section. Set only when sectionsClickable() is on.

The members of QStyleOptionHeader are:

Member

Description

section

The logical index of the section.

text

The section text.

textAlignment

The alignment of the text in the section.

icon

The section icon.

iconAlignment

The alignment of the icon in the section.

position

A SectionPosition value: the section’s position relative to the other sections.

selectedPosition

A SelectedPosition value: the position of the selected section relative to this one.

sortIndicator

A SortIndicator value: no indicator, or the direction of the sort arrow.

orientation

Whether this is the horizontal header above the view or the vertical header beside it.

QStyleOptionHeaderV2 adds textElideMode, the elide mode for text that doesn’t fit, and isSectionDragTarget, which is set on the section that a dragged section is about to be dropped on.

Tree branch indicators

QTreeView draws the branch indicators, the lines and arrows that show the relationship between the nodes, with PE_IndicatorBranch. It draws one indicator per indentation level in front of each item, so a deeply nested item has several. The tree also draws the background of each row with PE_PanelItemViewRow and indents the levels by PM_TreeViewIndentation.

Both elements take a QStyleOptionViewItem , so a style can also read the row’s palette, font, and alternate-row feature flag. The kind of branch is encoded in the state flags:

State

Set when

State_Item

The indicator belongs to the item in this row, rather than to an ancestor. Draw the horizontal connector here.

State_Children

The item has children, so the indicator shows an expand or collapse arrow.

State_Open

The item is expanded.

State_Sibling

The node at this level has a sibling below, so a vertical line continues downward.

An indicator with none of these flags is drawn for a level whose node has no sibling below; the style draws nothing there.

../_images/branchindicator.webp

Item view items

Delegates paint the items in item views. Qt’s default delegate, QStyledItemDelegate , draws CE_ItemViewItem with a QStyleOptionViewItem and calculates the item size with CT_ItemViewItem, so a style controls how items look without an accompanying delegate. QCommonStyle splits the element into the background, the check indicator, the decoration, the text, and the focus frame:

  • CE_ItemViewItem

    • PE_PanelItemViewItem

    • PE_IndicatorItemViewItemCheck (SE_ItemViewItemCheckIndicator)

    • The decoration, drawn with paint() (SE_ItemViewItemDecoration)

    • The text, laid out and drawn by QCommonStyle with eliding and wrapping (SE_ItemViewItemText)

    • PE_FrameFocusRect (SE_ItemViewItemFocusRect)

The views consult item view hints such as SH_ItemView_ShowDecorationSelected, SH_ItemView_ActivateItemOnSingleClick, and SH_ItemView_ArrowKeysNavigateIntoChildren. To support data types or roles that QStyledItemDelegate doesn’t handle, write a custom delegate; see Model/View Programming .

The state flags and members of QStyleOptionViewItem are described in its class documentation; the most important are State_Selected, State_HasFocus, checkState, decorationPosition, displayAlignment, features, icon, text, and viewItemPosition.