On this page

Styling Menus and Item Views

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.

QMenu keeps a list of actions and draws each as a menu item. On a paint event, it draws the panel of the menu, then each item with CE_MenuItem and a QStyleOptionMenuItem, and the frame last. Menu items have no separate label element, so CE_MenuItem draws the check mark, icon, text, shortcut, and submenu arrow.

  • PE_PanelMenu, the background
  • CE_MenuItem, once per action (PM_SmallIconSize)
    • PE_IndicatorMenuCheckMark, for a checked item. Many styles use PE_IndicatorCheckBox and PE_IndicatorRadioButton instead.
    • PE_IndicatorArrowRight or PE_IndicatorArrowLeft, for an item that opens a submenu
  • CE_MenuScroller (PM_MenuScrollerHeight), when the menu is too tall for the screen and SH_Menu_Scrollable is set
  • CE_MenuTearoff (PM_MenuTearoffHeight), for a tear-off menu
  • PE_FrameMenu (PM_MenuPanelWidth), the frame, with a QStyleOptionFrame whose lineWidth is PM_MenuPanelWidth
  • CE_MenuEmptyArea, for the space not covered by items or frame

QMenu lays out the items itself, using PM_MenuHMargin, PM_MenuVMargin, PM_MenuPanelWidth, and PM_MenuDesktopFrameWidth. It also consults many SH_Menu_ hints, such as SH_Menu_SubMenuPopupDelay, SH_Menu_MouseTracking, and SH_Menu_Mask.

Menu with an icon item, a checked item, a submenu item, a disabled item, and a plain item, with each item outlined

QMenu resets the state after calling initFrom(), so the items carry only State_Enabled, State_Active, and the flags in this table:

StateSet when
State_SelectedThe item is highlighted and isn't a separator.
State_SunkenThe item is pressed.
State_DownArrowThe item is a scroller that scrolls the menu down.
State_EnabledThe action is enabled. Disabled actions clear this flag even when the menu is enabled.

The members of QStyleOptionMenuItem are:

MemberDescription
menuItemTypeA MenuItemType value: a normal item, the default item, a separator, a submenu, a scroller, a tear-off, a margin, or an empty area.
checkTypeA CheckType value: not checkable, exclusive, or non-exclusive.
checkedWhether the item is checked.
menuHasCheckableItemsWhether at least one item in the menu is checkable, so that the style can reserve space for check marks.
menuRectThe rectangle of the whole menu.
textThe item text. A tab character separates the text from the shortcut.
iconThe item icon.
maxIconWidthThe width of the widest icon in the menu.
reservedShortcutWidthThe width reserved for the shortcut column.
fontThe font of the item text.

For CE_MenuScroller and CE_MenuTearoff, the menu sets only menuItemType, checkType, maxIconWidth, and reservedShortcutWidth. CE_MenuEmptyArea also sets menuRect.

QMenuBar draws each menu title with CE_MenuBarItem and a QStyleOptionMenuItemV2, the space after the last title with CE_MenuBarEmptyArea, and its panel with PE_PanelMenuBar and a QStyleOptionFrame. The drop-down menus are QMenus; see Menus.

  • CE_MenuBarItem, once per menu (PM_SmallIconSize)
  • PE_PanelMenuBar (PM_MenuBarPanelWidth)
  • CE_MenuBarEmptyArea

The menu bar lays out the items with PM_MenuBarItemSpacing, PM_MenuBarHMargin, and PM_MenuBarVMargin, and sizes them with CT_MenuBarItem. The painter that QMenuBar passes for the panel is clipped to the four border strips, each PM_MenuBarPanelWidth wide, so the panel can only paint the border. The painter for the empty area is clipped to what the items and the border leave over. The bar consults SH_MenuBar_MouseTracking, SH_MenuBar_AltKeyNavigation, and SH_DrawMenuBarSeparator.

On macOS, QMenuBar uses the native menu bar by default and doesn't draw anything. Call QMenuBar::setNativeMenuBar() to make it use the style.

Menu bar with the menus File, Edit, View, and Help, with each item and the empty area outlined

QMenuBar fills in the option itself rather than calling initFrom(). It sets State_Enabled when the bar is enabled, and these flags on the items:

StateSet when
State_SelectedThe item is highlighted.
State_SunkenThe item's menu is open.
State_HasFocusThe menu bar has focus or has a current item.
State_MouseOverThe mouse is over the item.

The menu bar uses these members of QStyleOptionMenuItem:

MemberDescription
menuRectThe rectangle of the whole menu bar.
textThe title of the menu.
iconThe icon of the menu. Few styles draw it.

For the panel, QStyleOptionFrame::lineWidth is set to PM_MenuBarPanelWidth and midLineWidth to 0.

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.

Table with the header sections Name, Size, and Modified, with the sections, labels, and the sort indicator outlined

The header sets these state flags on a section:

StateSet when
State_RaisedAlways. The header sets it for every section.
State_HorizontalThis is the horizontal header above the view.
State_SunkenThe section is pressed, or the section is selected and highlightSections is on.
State_OnAn item in the section is selected and highlightSections is on.
State_MouseOverThe mouse is over the section. Set only when sectionsClickable is on.

The members of QStyleOptionHeader are:

MemberDescription
sectionThe logical index of the section.
textThe section text.
textAlignmentThe alignment of the text in the section.
iconThe section icon.
iconAlignmentThe alignment of the icon in the section.
positionA SectionPosition value: the section's position relative to the other sections.
selectedPositionA SelectedPosition value: the position of the selected section relative to this one.
sortIndicatorA SortIndicator value: no indicator, or the direction of the sort arrow.
orientationWhether 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:

StateSet when
State_ItemThe indicator belongs to the item in this row, rather than to an ancestor. Draw the horizontal connector here.
State_ChildrenThe item has children, so the indicator shows an expand or collapse arrow.
State_OpenThe item is expanded.
State_SiblingThe 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.

Tree view with expanded and collapsed folders, with the branch indicator cells outlined and labeled by their state flags

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.

See also Widget Style Reference, Styling Buttons and Input Widgets, Styling Containers and Windows, and QStyledItemDelegate.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.