Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
styles-reference-menus-views.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page style-reference-menus-views.html
6
\title Styling Menus and Item Views
7
\brief The style elements, states, and options of menus, menu bars, item
8
view headers, tree branch indicators, and item view items.
9
10
This page is part of the \l{Widget Style Reference}. For an explanation
11
of the element trees, screenshots, and tables, see that page. The common
12
state flags and members that every widget sets are listed in
13
\l{How a Style Draws a Widget#Common state flags and members}{How a Style
14
Draws a Widget}.
15
16
\section1 Menus
17
18
\l QMenu keeps a list of actions and draws each as a menu item. On a paint
19
event, it draws the panel of the menu, then each item with \c CE_MenuItem
20
and a \l QStyleOptionMenuItem, and the frame last. Menu items have no
21
separate label element, so \c CE_MenuItem draws the check mark, icon, text,
22
shortcut, and submenu arrow.
23
24
\list
25
\li \c PE_PanelMenu, the background
26
\li \c CE_MenuItem, once per action (\c PM_SmallIconSize)
27
\list
28
\li \c PE_IndicatorMenuCheckMark, for a checked item. Many styles use
29
\c PE_IndicatorCheckBox and \c PE_IndicatorRadioButton instead.
30
\li \c PE_IndicatorArrowRight or \c PE_IndicatorArrowLeft, for an item
31
that opens a submenu
32
\endlist
33
\li \c CE_MenuScroller (\c PM_MenuScrollerHeight), when the menu is too tall
34
for the screen and \c SH_Menu_Scrollable is set
35
\li \c CE_MenuTearoff (\c PM_MenuTearoffHeight), for a
36
\l{QMenu::tearOffEnabled}{tear-off} menu
37
\li \c PE_FrameMenu (\c PM_MenuPanelWidth), the frame, with a
38
\l QStyleOptionFrame whose \c lineWidth is \c PM_MenuPanelWidth
39
\li \c CE_MenuEmptyArea, for the space not covered by items or frame
40
\endlist
41
42
\l QMenu lays out the items itself, using \c PM_MenuHMargin,
43
\c PM_MenuVMargin, \c PM_MenuPanelWidth, and \c PM_MenuDesktopFrameWidth. It
44
also consults many \c SH_Menu_ hints, such as \c SH_Menu_SubMenuPopupDelay,
45
\c SH_Menu_MouseTracking, and \c SH_Menu_Mask.
46
47
\image styles/menu.webp
48
{Menu with an icon item, a checked item, a submenu item, a disabled
49
item, and a plain item, with each item outlined}
50
51
\l QMenu resets the state after calling \l{QStyleOption::}{initFrom()}, so
52
the items carry only \c State_Enabled, \c State_Active, and the flags in
53
this table:
54
55
\table 90%
56
\header
57
\li State
58
\li Set when
59
\row
60
\li \c State_Selected
61
\li The item is highlighted and isn't a separator.
62
\row
63
\li \c State_Sunken
64
\li The item is pressed.
65
\row
66
\li \c State_DownArrow
67
\li The item is a scroller that scrolls the menu down.
68
\row
69
\li \c State_Enabled
70
\li The action is enabled. Disabled actions clear this flag even
71
when the menu is enabled.
72
\endtable
73
74
The members of \l QStyleOptionMenuItem are:
75
76
\table 90%
77
\header
78
\li Member
79
\li Description
80
\row
81
\li menuItemType
82
\li A \l{QStyleOptionMenuItem::}{MenuItemType} value: a normal item,
83
the default item, a separator, a submenu, a scroller, a
84
tear-off, a margin, or an empty area.
85
\row
86
\li checkType
87
\li A \l{QStyleOptionMenuItem::}{CheckType} value: not checkable,
88
exclusive, or non-exclusive.
89
\row
90
\li checked
91
\li Whether the item is checked.
92
\row
93
\li menuHasCheckableItems
94
\li Whether at least one item in the menu is checkable, so that the
95
style can reserve space for check marks.
96
\row
97
\li menuRect
98
\li The rectangle of the whole menu.
99
\row
100
\li text
101
\li The item text. A tab character separates the text from the
102
shortcut.
103
\row
104
\li icon
105
\li The item icon.
106
\row
107
\li maxIconWidth
108
\li The width of the widest icon in the menu.
109
\row
110
\li reservedShortcutWidth
111
\li The width reserved for the shortcut column.
112
\row
113
\li font
114
\li The font of the item text.
115
\endtable
116
117
For \c CE_MenuScroller and \c CE_MenuTearoff, the menu sets only
118
\c menuItemType, \c checkType, \c maxIconWidth, and
119
\c reservedShortcutWidth. \c CE_MenuEmptyArea also sets \c menuRect.
120
121
\section1 Menu bars
122
123
\l QMenuBar draws each menu title with \c CE_MenuBarItem and a
124
\l QStyleOptionMenuItemV2, the space after the last title with
125
\c CE_MenuBarEmptyArea, and its panel with \c PE_PanelMenuBar and a
126
\l QStyleOptionFrame. The drop-down menus are \l{QMenu}s; see \l{Menus}.
127
128
\list
129
\li \c CE_MenuBarItem, once per menu (\c PM_SmallIconSize)
130
\li \c PE_PanelMenuBar (\c PM_MenuBarPanelWidth)
131
\li \c CE_MenuBarEmptyArea
132
\endlist
133
134
The menu bar lays out the items with \c PM_MenuBarItemSpacing,
135
\c PM_MenuBarHMargin, and \c PM_MenuBarVMargin, and sizes them with
136
\c CT_MenuBarItem. The painter that \l QMenuBar passes for the panel is
137
clipped to the four border strips, each \c PM_MenuBarPanelWidth wide, so the
138
panel can only paint the border. The painter for the empty area is clipped
139
to what the items and the border leave over. The bar consults
140
\c SH_MenuBar_MouseTracking, \c SH_MenuBar_AltKeyNavigation, and
141
\c SH_DrawMenuBarSeparator.
142
143
On \macos, \l QMenuBar uses the native menu bar by default and doesn't draw
144
anything. Call \l{QMenuBar::setNativeMenuBar()} to make it use the style.
145
146
\image styles/menubar.webp
147
{Menu bar with the menus File, Edit, View, and Help, with each item
148
and the empty area outlined}
149
150
\l QMenuBar fills in the option itself rather than calling
151
\l{QStyleOption::}{initFrom()}. It sets \c State_Enabled when the bar is
152
enabled, and these flags on the items:
153
154
\table 90%
155
\header
156
\li State
157
\li Set when
158
\row
159
\li \c State_Selected
160
\li The item is highlighted.
161
\row
162
\li \c State_Sunken
163
\li The item's menu is open.
164
\row
165
\li \c State_HasFocus
166
\li The menu bar has focus or has a current item.
167
\row
168
\li \c State_MouseOver
169
\li The mouse is over the item.
170
\endtable
171
172
The menu bar uses these members of \l QStyleOptionMenuItem:
173
174
\table 90%
175
\header
176
\li Member
177
\li Description
178
\row
179
\li menuRect
180
\li The rectangle of the whole menu bar.
181
\row
182
\li text
183
\li The title of the menu.
184
\row
185
\li icon
186
\li The icon of the menu. Few styles draw it.
187
\endtable
188
189
For the panel, \l QStyleOptionFrame::lineWidth is set to
190
\c PM_MenuBarPanelWidth and \l{QStyleOptionFrame::}{midLineWidth} to 0.
191
192
\section1 Item view headers
193
194
\l QHeaderView draws each header section with \c CE_Header and a
195
\l QStyleOptionHeaderV2. \l QCommonStyle splits it into the section
196
background, the label, and the sort indicator:
197
198
\list
199
\li \c CE_Header, once per section (\c PM_HeaderMargin)
200
\list
201
\li \c CE_HeaderSection
202
\li \c CE_HeaderLabel (\c SE_HeaderLabel)
203
\li \c PE_IndicatorHeaderArrow (\c SE_HeaderArrow,
204
\c PM_HeaderMarkSize), for the sorted section
205
\endlist
206
\li \c CE_HeaderEmptyArea, for the space after the last section
207
\endlist
208
209
The header view sizes its sections with \c CT_HeaderSection and uses
210
\c PM_HeaderGripMargin for the area in which the user can drag a section
211
boundary. \l QTableView draws the button in the upper-left corner, where the
212
two headers meet, as a \c CE_Header. The hint \c SH_Header_ArrowAlignment
213
places the sort indicator.
214
215
\image styles/header.webp
216
{Table with the header sections Name, Size, and Modified, with the
217
sections, labels, and the sort indicator outlined}
218
219
The header sets these state flags on a section:
220
221
\table 90%
222
\header
223
\li State
224
\li Set when
225
\row
226
\li \c State_Raised
227
\li Always. The header sets it for every section.
228
\row
229
\li \c State_Horizontal
230
\li This is the horizontal header above the view.
231
\row
232
\li \c State_Sunken
233
\li The section is pressed, or the section is selected and
234
\l{QHeaderView::highlightSections}{highlightSections} is on.
235
\row
236
\li \c State_On
237
\li An item in the section is selected and
238
\l{QHeaderView::highlightSections}{highlightSections} is on.
239
\row
240
\li \c State_MouseOver
241
\li The mouse is over the section. Set only when
242
\l{QHeaderView::sectionsClickable}{sectionsClickable} is on.
243
\endtable
244
245
The members of \l QStyleOptionHeader are:
246
247
\table 90%
248
\header
249
\li Member
250
\li Description
251
\row
252
\li section
253
\li The logical index of the section.
254
\row
255
\li text
256
\li The section text.
257
\row
258
\li textAlignment
259
\li The alignment of the text in the section.
260
\row
261
\li icon
262
\li The section icon.
263
\row
264
\li iconAlignment
265
\li The alignment of the icon in the section.
266
\row
267
\li position
268
\li A \l{QStyleOptionHeader::}{SectionPosition} value: the section's
269
position relative to the other sections.
270
\row
271
\li selectedPosition
272
\li A \l{QStyleOptionHeader::}{SelectedPosition} value: the position
273
of the selected section relative to this one.
274
\row
275
\li sortIndicator
276
\li A \l{QStyleOptionHeader::}{SortIndicator} value: no indicator,
277
or the direction of the sort arrow.
278
\row
279
\li orientation
280
\li Whether this is the horizontal header above the view or the
281
vertical header beside it.
282
\endtable
283
284
\l QStyleOptionHeaderV2 adds \c textElideMode, the elide mode for text that
285
doesn't fit, and \c isSectionDragTarget, which is set on the section that a
286
dragged section is about to be dropped on.
287
288
\section1 Tree branch indicators
289
290
\l QTreeView draws the branch indicators, the lines and arrows that show the
291
relationship between the nodes, with \c PE_IndicatorBranch. It draws one
292
indicator per indentation level in front of each item, so a deeply nested
293
item has several. The tree also draws the background of each row with
294
\c PE_PanelItemViewRow and indents the levels by \c PM_TreeViewIndentation.
295
296
Both elements take a \l QStyleOptionViewItem, so a style can also read the
297
row's palette, font, and alternate-row feature flag. The kind of branch is
298
encoded in the state flags:
299
300
\table 90%
301
\header
302
\li State
303
\li Set when
304
\row
305
\li \c State_Item
306
\li The indicator belongs to the item in this row, rather than to an
307
ancestor. Draw the horizontal connector here.
308
\row
309
\li \c State_Children
310
\li The item has children, so the indicator shows an expand or
311
collapse arrow.
312
\row
313
\li \c State_Open
314
\li The item is expanded.
315
\row
316
\li \c State_Sibling
317
\li The node at this level has a sibling below, so a vertical line
318
continues downward.
319
\endtable
320
321
An indicator with none of these flags is drawn for a level whose node has no
322
sibling below; the style draws nothing there.
323
324
\image styles/branchindicator.webp
325
{Tree view with expanded and collapsed folders, with the branch
326
indicator cells outlined and labeled by their state flags}
327
328
\section1 Item view items
329
330
Delegates paint the items in item views. Qt's default delegate,
331
\l QStyledItemDelegate, draws \c CE_ItemViewItem with a
332
\l QStyleOptionViewItem and calculates the item size with
333
\c CT_ItemViewItem, so a style controls how items look without an
334
accompanying delegate. \l QCommonStyle splits the element into the
335
background, the check indicator, the decoration, the text, and the focus
336
frame:
337
338
\list
339
\li \c CE_ItemViewItem
340
\list
341
\li \c PE_PanelItemViewItem
342
\li \c PE_IndicatorItemViewItemCheck (\c SE_ItemViewItemCheckIndicator)
343
\li The decoration, drawn with \l{QIcon::}{paint()}
344
(\c SE_ItemViewItemDecoration)
345
\li The text, laid out and drawn by \l QCommonStyle with eliding and
346
wrapping (\c SE_ItemViewItemText)
347
\li \c PE_FrameFocusRect (\c SE_ItemViewItemFocusRect)
348
\endlist
349
\endlist
350
351
The views consult item view hints such as
352
\c SH_ItemView_ShowDecorationSelected,
353
\c SH_ItemView_ActivateItemOnSingleClick, and
354
\c SH_ItemView_ArrowKeysNavigateIntoChildren. To support data types or roles
355
that \l QStyledItemDelegate doesn't handle, write a custom delegate; see
356
\l{Model/View Programming}.
357
358
The state flags and members of \l QStyleOptionViewItem are described in its
359
class documentation; the most important are \c State_Selected,
360
\c State_HasFocus, \c checkState, \c decorationPosition,
361
\c displayAlignment, \c features, \c icon, \c text, and \c viewItemPosition.
362
363
\sa {Widget Style Reference}, {Styling Buttons and Input Widgets},
364
{Styling Containers and Windows}, QStyledItemDelegate
365
*/
qtbase
src
widgets
doc
src
widgets-and-layouts
styles-reference-menus-views.qdoc
Generated on
for Qt by
1.16.1