Table of Contents
- QMenu
- Synopsis
- Detailed Description
- Actions
- QMenu on macOS with Qt Build Against Cocoa
PySide2.QtWidgets.QMenuPySide2.QtWidgets.QMenu.aboutToHide()PySide2.QtWidgets.QMenu.aboutToShow()PySide2.QtWidgets.QMenu.actionAt()PySide2.QtWidgets.QMenu.actionGeometry()PySide2.QtWidgets.QMenu.activeAction()PySide2.QtWidgets.QMenu.addAction()PySide2.QtWidgets.QMenu.addMenu()PySide2.QtWidgets.QMenu.addSection()PySide2.QtWidgets.QMenu.addSeparator()PySide2.QtWidgets.QMenu.clear()PySide2.QtWidgets.QMenu.columnCount()PySide2.QtWidgets.QMenu.defaultAction()PySide2.QtWidgets.QMenu.exec_()PySide2.QtWidgets.QMenu.hideTearOffMenu()PySide2.QtWidgets.QMenu.hovered()PySide2.QtWidgets.QMenu.icon()PySide2.QtWidgets.QMenu.initStyleOption()PySide2.QtWidgets.QMenu.insertMenu()PySide2.QtWidgets.QMenu.insertSection()PySide2.QtWidgets.QMenu.insertSeparator()PySide2.QtWidgets.QMenu.isEmpty()PySide2.QtWidgets.QMenu.isTearOffEnabled()PySide2.QtWidgets.QMenu.isTearOffMenuVisible()PySide2.QtWidgets.QMenu.menuAction()PySide2.QtWidgets.QMenu.popup()PySide2.QtWidgets.QMenu.separatorsCollapsible()PySide2.QtWidgets.QMenu.setActiveAction()PySide2.QtWidgets.QMenu.setDefaultAction()PySide2.QtWidgets.QMenu.setIcon()PySide2.QtWidgets.QMenu.setSeparatorsCollapsible()PySide2.QtWidgets.QMenu.setTearOffEnabled()PySide2.QtWidgets.QMenu.setTitle()PySide2.QtWidgets.QMenu.setToolTipsVisible()PySide2.QtWidgets.QMenu.showTearOffMenu()PySide2.QtWidgets.QMenu.title()PySide2.QtWidgets.QMenu.toolTipsVisible()PySide2.QtWidgets.QMenu.triggered()
Previous topic
Next topic
Quick search
QMenu露

Synopsis露
Functions露
def
actionAt(arg__1)def
actionGeometry(arg__1)def
activeAction()def
addAction(arg__1, arg__2, arg__3[, arg__4=0])def
addAction(arg__1, arg__2[, arg__3=0])def
addAction(icon, text)def
addAction(icon, text, receiver, member[, shortcut=0])def
addAction(text)def
addAction(text, receiver, member[, shortcut=0])def
addMenu(icon, title)def
addMenu(menu)def
addMenu(title)def
addSection(icon, text)def
addSection(text)def
addSeparator()def
clear()def
columnCount()def
defaultAction()def
exec_()def
exec_(pos[, at=None])def
hideTearOffMenu()def
icon()def
initStyleOption(option, action)def
insertMenu(before, menu)def
insertSection(before, icon, text)def
insertSection(before, text)def
insertSeparator(before)def
isEmpty()def
isTearOffEnabled()def
isTearOffMenuVisible()def
menuAction()def
popup(pos[, at=None])def
separatorsCollapsible()def
setActiveAction(act)def
setDefaultAction(arg__1)def
setIcon(icon)def
setSeparatorsCollapsible(collapse)def
setTearOffEnabled(arg__1)def
setTitle(title)def
setToolTipsVisible(visible)def
showTearOffMenu()def
showTearOffMenu(pos)def
title()def
toolTipsVisible()
Signals露
def
aboutToHide()def
aboutToShow()def
hovered(action)def
triggered(action)
Static functions露
def
exec_(actions, pos[, at=None[, parent=None]])
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 鈥淐trl+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,QMenuprovides two signals,triggered()andhovered(), which signal theQActionthat was triggered from the menu.You clear a menu with
clear()and remove individual action items withremoveAction().A
QMenucan 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 鈥渢ear 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鈥檛 typically used on Microsoft Windows so some users may not be familiar with it. Consider using aQToolBarinstead.Widgets can be inserted into menus with the
QWidgetActionclass. Instances of this class are used to hold widgets, and are inserted into menus with theaddAction()overload that takes aQAction. If theQWidgetActionfires thetriggered()signal, the menu will close.
QMenu on macOS with Qt Build Against Cocoa露
QMenucan 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
QMenuBarandQMenuin your application.Important inherited functions:
addAction(),removeAction(),clear(),addSeparator(), andaddMenu().
- class PySide2.QtWidgets.QMenu([parent=None])露
PySide2.QtWidgets.QMenu(title[, parent=None])
- param parent:
- param title:
str
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).
- PySide2.QtWidgets.QMenu.aboutToHide()露
- PySide2.QtWidgets.QMenu.aboutToShow()露
- PySide2.QtWidgets.QMenu.actionAt(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtCore.QPoint- Return type:
Returns the item at
pt; returnsNoneif there is no item there.
- PySide2.QtWidgets.QMenu.actionGeometry(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtWidgets.QAction- Return type:
Returns the geometry of action
act.
- PySide2.QtWidgets.QMenu.activeAction()露
- Return type:
Returns the currently highlighted action, or
Noneif no action is currently highlighted.See also
- PySide2.QtWidgets.QMenu.addAction(text)露
- Parameters:
text 鈥 str
- Return type:
This convenience function creates a new action with
text. The function adds the newly created action to the menu鈥檚 list of actions, and returns it.QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.addAction(text, receiver, member[, shortcut=0])
- Parameters:
text 鈥 str
receiver 鈥
PySide2.QtCore.QObjectmember 鈥 str
shortcut 鈥
PySide2.QtGui.QKeySequence
- Return type:
- PySide2.QtWidgets.QMenu.addAction(icon, text, receiver, member[, shortcut=0])
- Parameters:
icon 鈥
PySide2.QtGui.QIcontext 鈥 str
receiver 鈥
PySide2.QtCore.QObjectmember 鈥 str
shortcut 鈥
PySide2.QtGui.QKeySequence
- Return type:
- PySide2.QtWidgets.QMenu.addAction(icon, text)
- Parameters:
icon 鈥
PySide2.QtGui.QIcontext 鈥 str
- Return type:
This is an overloaded function.
This convenience function creates a new action with an
iconand sometext. The function adds the newly created action to the menu鈥檚 list of actions, and returns it.QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.addAction(arg__1, arg__2[, arg__3=0])
- Parameters:
arg__1 鈥 str
arg__2 鈥 object
arg__3 鈥
PySide2.QtGui.QKeySequence
- PySide2.QtWidgets.QMenu.addAction(arg__1, arg__2, arg__3[, arg__4=0])
- Parameters:
arg__1 鈥
PySide2.QtGui.QIconarg__2 鈥 str
arg__3 鈥 object
arg__4 鈥
PySide2.QtGui.QKeySequence
- PySide2.QtWidgets.QMenu.addMenu(menu)露
- Parameters:
menu 鈥
PySide2.QtWidgets.QMenu- Return type:
This convenience function adds
menuas a submenu to this menu. It returnsmenu鈥榮menuAction(). This menu does not take ownership ofmenu.See also
- PySide2.QtWidgets.QMenu.addMenu(icon, title)
- Parameters:
icon 鈥
PySide2.QtGui.QIcontitle 鈥 str
- Return type:
Appends a new
QMenuwithiconandtitleto the menu. The menu takes ownership of the menu. Returns the new menu.See also
- PySide2.QtWidgets.QMenu.addMenu(title)
- Parameters:
title 鈥 str
- Return type:
- PySide2.QtWidgets.QMenu.addSection(icon, text)露
- Parameters:
icon 鈥
PySide2.QtGui.QIcontext 鈥 str
- Return type:
This convenience function creates a new section action, i.e. an action with
isSeparator()returning true but also havingtextandiconhints, and adds the new action to this menu鈥檚 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.
QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.addSection(text)
- Parameters:
text 鈥 str
- Return type:
This convenience function creates a new section action, i.e. an action with
isSeparator()returning true but also havingtexthint, and adds the new action to this menu鈥檚 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.
QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.addSeparator()露
- Return type:
This convenience function creates a new separator action, i.e. an action with
isSeparator()returning true, and adds the new action to this menu鈥檚 list of actions. It returns the newly created action.QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.clear()露
Removes all the menu鈥檚 actions. Actions owned by the menu and not shown in any other widget are deleted.
See also
- PySide2.QtWidgets.QMenu.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.
- PySide2.QtWidgets.QMenu.defaultAction()露
- Return type:
Returns the current default action.
See also
- PySide2.QtWidgets.QMenu.exec_(pos[, at=None])露
- Parameters:
pos 鈥
PySide2.QtCore.QPoint
- Return type:
This is an overloaded function.
Executes this menu synchronously.
Pops up the menu so that the action
actionwill be at the specified global positionp. To translate a widget鈥檚 local coordinates into global coordinates, usemapToGlobal().This returns the triggered
QActionin either the popup menu or one of its submenus, orNoneif no item was triggered (normally because the user pressed Esc).Note that all signals are emitted as usual. If you connect a
QActionto a slot and call the menu鈥檚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.globalPos())
When positioning a menu with
exec()orpopup(), bear in mind that you cannot rely on the menu鈥檚 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鈥檚 current contents.See also
- static PySide2.QtWidgets.QMenu.exec_(actions, pos[, at=None[, parent=None]])
- Parameters:
actions 鈥
pos 鈥
PySide2.QtCore.QPointparent 鈥
PySide2.QtWidgets.QWidget
- Return type:
This is an overloaded function.
Executes a menu synchronously.
The menu鈥檚 actions are specified by the list of
actions. The menu will pop up so that the specified action,at, appears at global positionpos. Ifatis not specified then the menu appears at positionpos.parentis the menu鈥檚 parent widget; specifying the parent will provide context whenposalone 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
QActionin either the popup menu or one of its submenus, orNoneif 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
- PySide2.QtWidgets.QMenu.exec_()
- Return type:
Executes this menu synchronously.
This is equivalent to
exec(pos()).This returns the triggered
QActionin either the popup menu or one of its submenus, orNoneif no item was triggered (normally because the user pressed Esc).In most situations you鈥檒l 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.globalPos())
- PySide2.QtWidgets.QMenu.hideTearOffMenu()露
This function will forcibly hide the torn off menu making it disappear from the user鈥檚 desktop.
- PySide2.QtWidgets.QMenu.hovered(action)露
- Parameters:
action 鈥
PySide2.QtWidgets.QAction
- PySide2.QtWidgets.QMenu.icon()露
- Return type:
This property holds The icon of the menu.
This is equivalent to the
iconproperty of themenuAction().By default, if no icon is explicitly set, this property contains a null icon.
- PySide2.QtWidgets.QMenu.initStyleOption(option, action)露
- Parameters:
action 鈥
PySide2.QtWidgets.QAction
Initialize
optionwith the values from this menu and information fromaction. This method is useful for subclasses when they need aQStyleOptionMenuItem, but don鈥檛 want to fill in all the information themselves.See also
- PySide2.QtWidgets.QMenu.insertMenu(before, menu)露
- Parameters:
before 鈥
PySide2.QtWidgets.QActionmenu 鈥
PySide2.QtWidgets.QMenu
- Return type:
This convenience function inserts
menubefore actionbeforeand returns the menusmenuAction().See also
- PySide2.QtWidgets.QMenu.insertSection(before, icon, text)露
- Parameters:
before 鈥
PySide2.QtWidgets.QActionicon 鈥
PySide2.QtGui.QIcontext 鈥 str
- Return type:
This convenience function creates a new title action, i.e. an action with
isSeparator()returning true but also havingtextandiconhints. The function inserts the newly created action into this menu鈥檚 list of actions before actionbeforeand 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.
QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.insertSection(before, text)
- Parameters:
before 鈥
PySide2.QtWidgets.QActiontext 鈥 str
- Return type:
This convenience function creates a new title action, i.e. an action with
isSeparator()returning true but also havingtexthint. The function inserts the newly created action into this menu鈥檚 list of actions before actionbeforeand 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.
QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.insertSeparator(before)露
- Parameters:
before 鈥
PySide2.QtWidgets.QAction- Return type:
This convenience function creates a new separator action, i.e. an action with
isSeparator()returning true. The function inserts the newly created action into this menu鈥檚 list of actions before actionbeforeand returns it.QMenutakes ownership of the returnedQAction.See also
- PySide2.QtWidgets.QMenu.isEmpty()露
- Return type:
bool
Returns
trueif there are no visible actions inserted into the menu, false otherwise.See also
- PySide2.QtWidgets.QMenu.isTearOffEnabled()露
- Return type:
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 鈥渢orn-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.
- PySide2.QtWidgets.QMenu.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.
- PySide2.QtWidgets.QMenu.menuAction()露
- Return type:
Returns the action associated with this menu.
- PySide2.QtWidgets.QMenu.popup(pos[, at=None])露
- Parameters:
pos 鈥
PySide2.QtCore.QPoint
Displays the menu so that the action
atActionwill be at the specified global positionp. To translate a widget鈥檚 local coordinates into global coordinates, usemapToGlobal().When positioning a menu with
exec()or , bear in mind that you cannot rely on the menu鈥檚 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鈥檚 current contents.See also
mapToGlobal()exec()
- PySide2.QtWidgets.QMenu.separatorsCollapsible()露
- Return type:
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.
- PySide2.QtWidgets.QMenu.setActiveAction(act)露
- Parameters:
Sets the currently highlighted action to
act.See also
- PySide2.QtWidgets.QMenu.setDefaultAction(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtWidgets.QAction
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
- PySide2.QtWidgets.QMenu.setIcon(icon)露
- Parameters:
icon 鈥
PySide2.QtGui.QIcon
This property holds The icon of the menu.
This is equivalent to the
iconproperty of themenuAction().By default, if no icon is explicitly set, this property contains a null icon.
- PySide2.QtWidgets.QMenu.setSeparatorsCollapsible(collapse)露
- Parameters:
collapse 鈥 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.
- PySide2.QtWidgets.QMenu.setTearOffEnabled(arg__1)露
- Parameters:
arg__1 鈥 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 鈥渢orn-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.
- PySide2.QtWidgets.QMenu.setTitle(title)露
- Parameters:
title 鈥 str
This property holds The title of the menu.
This is equivalent to the
textproperty of themenuAction().By default, this property contains an empty string.
- PySide2.QtWidgets.QMenu.setToolTipsVisible(visible)露
- Parameters:
visible 鈥 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.
- PySide2.QtWidgets.QMenu.showTearOffMenu()露
This is an overloaded function.
This function will forcibly show the torn off menu making it appear on the user鈥檚 desktop under the mouse currsor.
- PySide2.QtWidgets.QMenu.showTearOffMenu(pos)
- Parameters:
pos 鈥
PySide2.QtCore.QPoint
This function will forcibly show the torn off menu making it appear on the user鈥檚 desktop at the specified global position
pos.
- PySide2.QtWidgets.QMenu.title()露
- Return type:
str
This property holds The title of the menu.
This is equivalent to the
textproperty of themenuAction().By default, this property contains an empty string.
- PySide2.QtWidgets.QMenu.toolTipsVisible()露
- Return type:
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.
- PySide2.QtWidgets.QMenu.triggered(action)露
- Parameters:
action 鈥
PySide2.QtWidgets.QAction
漏 2022 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.