PySide6.QtWidgets.QMenu¶
- class QMenu¶
The
QMenu
class provides a menu widget for use in menu bars, context menus, and other popup menus. More…Synopsis¶
Properties¶
iconᅟ
- Of the menuseparatorsCollapsibleᅟ
- Whether consecutive separators should be collapsedtearOffEnabledᅟ
- Whether the menu supports being torn offtitleᅟ
- Of the menutoolTipsVisibleᅟ
- Whether tooltips of menu actions should be visible
Methods¶
def
__init__()
def
actionAt()
def
actionGeometry()
def
activeAction()
def
addAction()
def
addMenu()
def
addSection()
def
addSeparator()
def
clear()
def
columnCount()
def
defaultAction()
def
exec()
def
exec_()
def
icon()
def
insertMenu()
def
insertSection()
def
isEmpty()
def
menuAction()
def
popup()
def
setIcon()
def
setTitle()
def
title()
Virtual methods¶
Signals¶
def
aboutToHide()
def
aboutToShow()
def
hovered()
def
triggered()
Static functions¶
def
exec()
def
menuInAction()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
A menu widget is a selection menu. It can be either a pull-down menu in a menu bar or a standalone context menu. Pull-down menus are shown by the menu bar when the user clicks on the respective item or presses the specified shortcut key. Use
addMenu()
to insert a menu into a menu bar. Context menus are usually invoked by some special keyboard key or by right-clicking. They can be executed either asynchronously withpopup()
or synchronously withexec()
. Menus can also be invoked in response to button presses; these are just like context menus except for how they are invoked.Actions¶
A menu consists of a list of action items. Actions are added with the addAction(),
addActions()
andinsertAction()
functions. An action is represented vertically and rendered byQStyle
. In addition, actions can have a text label, an optional icon drawn on the very left side, and shortcut key sequence such as “Ctrl+X”.The existing actions held by a menu can be found with
actions()
.There are four kinds of action items: separators, actions that show a submenu, widgets, and actions that perform an action. Separators are inserted with
addSeparator()
, submenus withaddMenu()
, and all other items are considered action items.When inserting action items you usually specify a receiver and a slot. The receiver will be notified whenever the item is triggered(). In addition,
QMenu
provides two signals,triggered()
andhovered()
, which signal the QAction that was triggered from the menu.You clear a menu with
clear()
and remove individual action items withremoveAction()
.A
QMenu
can also provide a tear-off menu. A tear-off menu is a top-level window that contains a copy of the menu. This makes it possible for the user to “tear off” frequently used menus and position them in a convenient place on the screen. If you want this functionality for a particular menu, insert a tear-off handle withsetTearOffEnabled()
. When using tear-off menus, bear in mind that the concept isn’t typically used on Microsoft Windows so some users may not be familiar with it. Consider using aQToolBar
instead.Widgets can be inserted into menus with the
QWidgetAction
class. Instances of this class are used to hold widgets, and are inserted into menus with the addAction() overload that takes a QAction. If theQWidgetAction
fires thetriggered()
signal, the menu will close.Warning
To make
QMenu
visible on the screen,exec()
orpopup()
should be used instead ofshow()
orsetVisible()
. To hide or disable the menu in the menubar, or in another menu to which it was added as a submenu, use the respective properties ofmenuAction()
instead.QMenu on macOS with Qt Build Against Cocoa¶
QMenu
can be inserted only once in a menu/menubar. Subsequent insertions will have no effect or will result in a disabled menu item.See the Menus example for an example of how to use
QMenuBar
andQMenu
in your application.Important inherited functions: addAction(),
removeAction()
,clear()
,addSeparator()
, andaddMenu()
.See also
QMenuBar
Menus ExampleNote
Properties can be used directly when
from __feature__ import true_property
is used or via accessor functions otherwise.This property holds The icon of the menu.
This is equivalent to the QAction::icon property of the
menuAction()
.By default, if no icon is explicitly set, this property contains a null icon.
- property separatorsCollapsibleᅟ: bool¶
This property holds whether consecutive separators should be collapsed.
This property specifies whether consecutive separators in the menu should be visually collapsed to a single one. Separators at the beginning or the end of the menu are also hidden.
By default, this property is
true
.- Access functions:
- property tearOffEnabledᅟ: bool¶
This property holds whether the menu supports being torn off.
When true, the menu contains a special tear-off item (often shown as a dashed line at the top of the menu) that creates a copy of the menu when it is triggered.
This “torn-off” copy lives in a separate window. It contains the same menu items as the original menu, with the exception of the tear-off handle.
By default, this property is
false
.- Access functions:
- property titleᅟ: str¶
This property holds The title of the menu.
This is equivalent to the QAction::text property of the
menuAction()
.By default, this property contains an empty string.
- Access functions:
- property toolTipsVisibleᅟ: bool¶
This property holds whether tooltips of menu actions should be visible.
This property specifies whether action menu entries show their tooltip.
By default, this property is
false
.- Access functions:
Constructs a menu with parent
parent
.Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject).
- __init__(title[, parent=None])
- Parameters:
title – str
parent –
QWidget
Constructs a menu with a
title
and aparent
.Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject).
See also
- aboutToHide()¶
This signal is emitted just before the menu is hidden from the user.
See also
- aboutToShow()¶
This signal is emitted just before the menu is shown to the user.
See also
Returns the item at
pt
; returnsNone
if there is no item there.Returns the geometry of action
act
.Returns the currently highlighted action, or
None
if no action is currently highlighted.See also
- addAction(text, arg__2[, shortcut=0])¶
- Parameters:
text – str
arg__2 – object
shortcut –
QKeySequence
- addAction(arg__1, text, arg__3[, shortcut=0])
- Parameters:
arg__1 –
QIcon
text – str
arg__3 – object
shortcut –
QKeySequence
- addAction(text, receiver, member, shortcut)
- Parameters:
text – str
receiver –
QObject
member – str
shortcut –
QKeySequence
- Return type:
Note
This function is deprecated.
Use
QWidget::addAction(text, shortcut, receiver, member)
instead.- addAction(icon, text, receiver, member, shortcut)
- Parameters:
icon –
QIcon
text – str
receiver –
QObject
member – str
shortcut –
QKeySequence
- Return type:
Note
This function is deprecated.
Use
addAction
(icon, text, shortcut, receiver, member) instead.This convenience function adds
menu
as a submenu to this menu. It returnsmenu
'smenuAction()
. This menu does not take ownership ofmenu
.See also
- addMenu(title)
- Parameters:
title – str
- Return type:
Appends a new
QMenu
withtitle
to the menu. The menu takes ownership of the menu. Returns the new menu.See also
Appends a new
QMenu
withicon
andtitle
to the menu. The menu takes ownership of the menu. Returns the new menu.See also
This convenience function creates a new section action, i.e. an action with QAction::isSeparator() returning true but also having
text
hint, and adds the new action to this menu’s list of actions. It returns the newly created action.The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
QMenu
takes ownership of the returned QAction.See also
This convenience function creates a new section action, i.e. an action with QAction::isSeparator() returning true but also having
text
andicon
hints, and adds the new action to this menu’s list of actions. It returns the newly created action.The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.
QMenu
takes ownership of the returned QAction.See also
This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true, and adds the new action to this menu’s list of actions. It returns the newly created action.
QMenu
takes ownership of the returned QAction.See also
- clear()¶
Removes all the menu’s actions. Actions owned by the menu and not shown in any other widget are deleted.
See also
- columnCount()¶
- Return type:
int
If a menu does not fit on the screen it lays itself out so that it does fit. It is style dependent what layout means (for example, on Windows it will use multiple columns).
This functions returns the number of columns necessary.
Returns the current default action.
See also
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Executes this menu synchronously.
This is equivalent to
exec(pos())
.This returns the triggered QAction in either the popup menu or one of its submenus, or
None
if no item was triggered (normally because the user pressed Esc).In most situations you’ll want to specify the position yourself, for example, the current mouse position:
exec(QCursor.pos())
or aligned to a widget:
exec(somewidget.mapToGlobal(QPoint(0,0)))
or in reaction to a QMouseEvent *e:
exec(e.globalPosition().toPoint())
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
This is an overloaded function.
Executes this menu synchronously.
Pops up the menu so that the action
action
will be at the specified global positionp
. To translate a widget’s local coordinates into global coordinates, usemapToGlobal()
.This returns the triggered QAction in either the popup menu or one of its submenus, or
None
if no item was triggered (normally because the user pressed Esc).Note that all signals are emitted as usual. If you connect a QAction to a slot and call the menu’s
exec()
, you get the result both via the signal-slot connection and in the return value ofexec()
.Common usage is to position the menu at the current mouse position:
exec(QCursor.pos())
or aligned to a widget:
exec(somewidget.mapToGlobal(QPoint(0, 0)))
or in reaction to a QMouseEvent *e:
exec(e.globalPosition().toPoint())
When positioning a menu with
exec()
orpopup()
, bear in mind that you cannot rely on the menu’s currentsize()
. For performance reasons, the menu adapts its size only when necessary. So in many cases, the size before and after the show is different. Instead, usesizeHint()
which calculates the proper size depending on the menu’s current contents.See also
- static exec(actions, pos[, at=None[, parent=None]])
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
This is an overloaded function.
Executes a menu synchronously.
The menu’s actions are specified by the list of
actions
. The menu will pop up so that the specified action,at
, appears at global positionpos
. Ifat
is not specified then the menu appears at positionpos
.parent
is the menu’s parent widget; specifying the parent will provide context whenpos
alone is not enough to decide where the menu should go (e.g., with multiple desktops or when the parent is embedded inQGraphicsView
).The function returns the triggered QAction in either the popup menu or one of its submenus, or
None
if no item was triggered (normally because the user pressed Esc).This is equivalent to:
menu = QMenu() at = actions[0] # Assumes actions is not empty for a in actions: menu.addAction(a) menu.exec(pos, at)
See also
- exec_(arg__1, arg__2[, at=None[, parent=None]])
- hideTearOffMenu()¶
This function will forcibly hide the torn off menu making it disappear from the user’s desktop.
This signal is emitted when a menu action is highlighted;
action
is the action that caused the signal to be emitted.Often this is used to update status information.
See also
Getter of property
iconᅟ
.- initStyleOption(option, action)¶
- Parameters:
option –
QStyleOptionMenuItem
action –
QAction
Initialize
option
with the values from this menu and information fromaction
. This method is useful for subclasses when they need aQStyleOptionMenuItem
, but don’t want to fill in all the information themselves.See also
This convenience function inserts
menu
before actionbefore
and returns the menusmenuAction()
.See also
This convenience function creates a new title action, i.e. an action with QAction::isSeparator() returning true but also having
text
hint. The function inserts the newly created action into this menu’s list of actions before actionbefore
and returns it.The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
QMenu
takes ownership of the returned QAction.See also
- insertSection(before, icon, text)
This convenience function creates a new title action, i.e. an action with QAction::isSeparator() returning true but also having
text
andicon
hints. The function inserts the newly created action into this menu’s list of actions before actionbefore
and returns it.The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.
QMenu
takes ownership of the returned QAction.See also
This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true. The function inserts the newly created action into this menu’s list of actions before action
before
and returns it.QMenu
takes ownership of the returned QAction.See also
- isEmpty()¶
- Return type:
bool
Returns
true
if there are no visible actions inserted into the menu, false otherwise.See also
- isTearOffEnabled()¶
- Return type:
bool
Getter of property
tearOffEnabledᅟ
.- isTearOffMenuVisible()¶
- Return type:
bool
When a menu is torn off a second menu is shown to display the menu contents in a new window. When the menu is in this mode and the menu is visible returns
true
; otherwise false.Returns the action associated with this menu.
Returns the menu contained by
action
, orNone
ifaction
does not contain a menu.In widget applications, actions that contain menus can be used to create menu items with submenus, or inserted into toolbars to create buttons with popup menus.
Displays the menu so that the action
atAction
will be at the specified global positionp
. To translate a widget’s local coordinates into global coordinates, usemapToGlobal()
.When positioning a menu with
exec()
or popup(), bear in mind that you cannot rely on the menu’s currentsize()
. For performance reasons, the menu adapts its size only when necessary, so in many cases, the size before and after the show is different. Instead, usesizeHint()
which calculates the proper size depending on the menu’s current contents.See also
- separatorsCollapsible()¶
- Return type:
bool
See also
Getter of property
separatorsCollapsibleᅟ
.Sets the currently highlighted action to
act
.See also
This sets the default action to
act
. The default action may have a visual cue, depending on the currentQStyle
. A default action usually indicates what will happen by default when a drop occurs.See also
Setter of property
iconᅟ
.- setSeparatorsCollapsible(collapse)¶
- Parameters:
collapse – bool
See also
Setter of property
separatorsCollapsibleᅟ
.- setTearOffEnabled(arg__1)¶
- Parameters:
arg__1 – bool
See also
Setter of property
tearOffEnabledᅟ
.Setter of property
titleᅟ
.- setToolTipsVisible(visible)¶
- Parameters:
visible – bool
See also
Setter of property
toolTipsVisibleᅟ
.- showTearOffMenu()¶
This is an overloaded function.
This function will forcibly show the torn off menu making it appear on the user’s desktop under the mouse currsor.
- showTearOffMenu(pos)
- Parameters:
pos –
QPoint
This function will forcibly show the torn off menu making it appear on the user’s desktop at the specified global position
pos
.- title()¶
- Return type:
str
See also
Getter of property
titleᅟ
.- toolTipsVisible()¶
- Return type:
bool
See also
Getter of property
toolTipsVisibleᅟ
.This signal is emitted when an action in this menu is triggered.
action
is the action that caused the signal to be emitted.Normally, you connect each menu action’s triggered() signal to its own custom slot, but sometimes you will want to connect several actions to a single slot, for example, when you have a group of closely related actions, such as “left justify”, “center”, “right justify”.
Note
This signal is emitted for the main parent menu in a hierarchy. Hence, only the parent menu needs to be connected to a slot; sub-menus need not be connected.
See also