QMenu¶
The QMenu
class provides a menu widget for use in menu bars, context menus, and other popup menus. More…
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={}])def
addAction
(text)def
addAction
(text, receiver, member[, shortcut={}])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
exec_
()def
exec_
(arg__1, arg__2[, at=None[, parent=None]])def
exec_
(arg__1[, action=None])def
hideTearOffMenu
()def
icon
()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
()
Virtual functions¶
def
initStyleOption
(option, action)
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 with popup()
or synchronously with exec()
. 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()
and insertAction()
functions. An action is represented vertically and rendered by QStyle
. 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 with addMenu()
, 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()
and hovered()
, which signal the QAction
that was triggered from the menu.
You clear a menu with clear()
and remove individual action items with removeAction()
.
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 with setTearOffEnabled()
. 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 a QToolBar
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 the QWidgetAction
fires the triggered()
signal, the menu will close.
Warning
To make QMenu
visible on the screen, exec()
or popup()
should be used instead of show()
or setVisible()
. 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 of menuAction()
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
and QMenu
in your application.
Important inherited functions: addAction()
, removeAction()
, clear()
, addSeparator()
, and addMenu()
.
See also
QMenuBar
GUI Design Handbook: Menu, Drop-Down and Pop-Up Qt Widgets - Application Example Menus Example
- class PySide6.QtWidgets.QMenu([parent=None])¶
PySide6.QtWidgets.QMenu(title[, parent=None])
- Parameters
parent –
PySide6.QtWidgets.QWidget
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
).
Constructs a menu with a title
and a 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
).
See also
- PySide6.QtWidgets.QMenu.aboutToHide()¶
- PySide6.QtWidgets.QMenu.aboutToShow()¶
- PySide6.QtWidgets.QMenu.actionAt(arg__1)¶
- Parameters
arg__1 –
PySide6.QtCore.QPoint
- Return type
Returns the item at pt
; returns None
if there is no item there.
- PySide6.QtWidgets.QMenu.actionGeometry(arg__1)¶
- Parameters
arg__1 –
PySide6.QtGui.QAction
- Return type
Returns the geometry of action act
.
- PySide6.QtWidgets.QMenu.activeAction()¶
- Return type
Returns the currently highlighted action, or None
if no action is currently highlighted.
See also
- PySide6.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’s list of actions, and returns it.
QMenu
takes ownership of the returned QAction
.
See also
- PySide6.QtWidgets.QMenu.addAction(text, receiver, member[, shortcut={}])
- Parameters
text – str
receiver –
PySide6.QtCore.QObject
member – str
shortcut –
PySide6.QtGui.QKeySequence
- Return type
This is an overloaded function.
This convenience function creates a new action with the text text
and an optional shortcut shortcut
. The action’s triggered()
signal is connected to the receiver
's member
slot. The function adds the newly created action to the menu’s list of actions and returns it.
QMenu
takes ownership of the returned QAction
.
See also
- PySide6.QtWidgets.QMenu.addAction(icon, text, receiver, member[, shortcut={}])
- Parameters
icon –
PySide6.QtGui.QIcon
text – str
receiver –
PySide6.QtCore.QObject
member – str
shortcut –
PySide6.QtGui.QKeySequence
- Return type
This is an overloaded function.
This convenience function creates a new action with an icon
and some text
and an optional shortcut shortcut
. The action’s triggered()
signal is connected to the member
slot of the receiver
object. The function adds the newly created action to the menu’s list of actions, and returns it.
QMenu
takes ownership of the returned QAction
.
See also
- PySide6.QtWidgets.QMenu.addAction(icon, text)
- Parameters
icon –
PySide6.QtGui.QIcon
text – str
- Return type
This is an overloaded function.
This convenience function creates a new action with an icon
and some text
. The function adds the newly created action to the menu’s list of actions, and returns it.
QMenu
takes ownership of the returned QAction
.
See also
- PySide6.QtWidgets.QMenu.addAction(arg__1, arg__2[, arg__3=0])
- Parameters
arg__1 – str
arg__2 – object
arg__3 –
PySide6.QtGui.QKeySequence
- PySide6.QtWidgets.QMenu.addAction(arg__1, arg__2, arg__3[, arg__4=0])
- Parameters
arg__1 –
PySide6.QtGui.QIcon
arg__2 – str
arg__3 – object
arg__4 –
PySide6.QtGui.QKeySequence
- PySide6.QtWidgets.QMenu.addMenu(menu)¶
- Parameters
menu –
PySide6.QtWidgets.QMenu
- Return type
This convenience function adds menu
as a submenu to this menu. It returns menu
's menuAction()
. This menu does not take ownership of menu
.
See also
- PySide6.QtWidgets.QMenu.addMenu(icon, title)
- Parameters
icon –
PySide6.QtGui.QIcon
title – str
- Return type
Appends a new QMenu
with icon
and title
to the menu. The menu takes ownership of the menu. Returns the new menu.
See also
- PySide6.QtWidgets.QMenu.addMenu(title)
- Parameters
title – str
- Return type
Appends a new QMenu
with title
to the menu. The menu takes ownership of the menu. Returns the new menu.
See also
- PySide6.QtWidgets.QMenu.addSection(icon, text)¶
- Parameters
icon –
PySide6.QtGui.QIcon
text – str
- Return type
This convenience function creates a new section action, i.e. an action with isSeparator()
returning true but also having text
and icon
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
- PySide6.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 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
- PySide6.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’s list of actions. It returns the newly created action.
QMenu
takes ownership of the returned QAction
.
See also
- PySide6.QtWidgets.QMenu.clear()¶
Removes all the menu’s actions. Actions owned by the menu and not shown in any other widget are deleted.
See also
- PySide6.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.
- PySide6.QtWidgets.QMenu.defaultAction()¶
- Return type
Returns the current default action.
See also
- PySide6.QtWidgets.QMenu.exec(pos[, at=None])¶
- Parameters
pos –
PySide6.QtCore.QPoint
- Return type
This is an overloaded function.
Executes this menu synchronously.
Pops up the menu so that the action action
will be at the specified global position p
. To translate a widget’s local coordinates into global coordinates, use mapToGlobal()
.
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 of exec()
.
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()
or popup()
, bear in mind that you cannot rely on the menu’s current size()
. 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, use sizeHint()
which calculates the proper size depending on the menu’s current contents.
See also
- PySide6.QtWidgets.QMenu.exec()
- Return type
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.globalPos())
- static PySide6.QtWidgets.QMenu.exec(actions, pos[, at=None[, parent=None]])
- Parameters
actions –
pos –
PySide6.QtCore.QPoint
parent –
PySide6.QtWidgets.QWidget
- Return type
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 position pos
. If at
is not specified then the menu appears at position pos
. parent
is the menu’s parent widget; specifying the parent will provide context when pos
alone is not enough to decide where the menu should go (e.g., with multiple desktops or when the parent is embedded in QGraphicsView
).
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 qAsConst(actions): menu.addAction(a) menu.exec(pos, at)See also
- PySide6.QtWidgets.QMenu.exec_()¶
- Return type
- PySide6.QtWidgets.QMenu.exec_(arg__1, arg__2[, at=None[, parent=None]])
- Parameters
arg__1 –
arg__2 –
PySide6.QtCore.QPoint
parent –
PySide6.QtWidgets.QWidget
- Return type
- PySide6.QtWidgets.QMenu.exec_(arg__1[, action=None])
- Parameters
arg__1 –
PySide6.QtCore.QPoint
action –
PySide6.QtGui.QAction
- Return type
- PySide6.QtWidgets.QMenu.hideTearOffMenu()¶
This function will forcibly hide the torn off menu making it disappear from the user’s desktop.
- PySide6.QtWidgets.QMenu.hovered(action)¶
- Parameters
action –
PySide6.QtGui.QAction
- PySide6.QtWidgets.QMenu.icon()¶
- Return type
This property holds The icon of the menu.
This is equivalent to the icon
property of the menuAction()
.
By default, if no icon is explicitly set, this property contains a null icon.
- PySide6.QtWidgets.QMenu.initStyleOption(option, action)¶
- Parameters
action –
PySide6.QtGui.QAction
Initialize option
with the values from this menu and information from action
. This method is useful for subclasses when they need a QStyleOptionMenuItem
, but don’t want to fill in all the information themselves.
See also
- PySide6.QtWidgets.QMenu.insertMenu(before, menu)¶
- Parameters
before –
PySide6.QtGui.QAction
menu –
PySide6.QtWidgets.QMenu
- Return type
This convenience function inserts menu
before action before
and returns the menus menuAction()
.
See also
- PySide6.QtWidgets.QMenu.insertSection(before, icon, text)¶
- Parameters
before –
PySide6.QtGui.QAction
icon –
PySide6.QtGui.QIcon
text – str
- Return type
This convenience function creates a new title action, i.e. an action with isSeparator()
returning true but also having text
and icon
hints. The function inserts the newly created action into this menu’s list of actions before action before
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
- PySide6.QtWidgets.QMenu.insertSection(before, text)
- Parameters
before –
PySide6.QtGui.QAction
text – str
- Return type
This convenience function creates a new title action, i.e. an action with isSeparator()
returning true but also having text
hint. The function inserts the newly created action into this menu’s list of actions before action before
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
- PySide6.QtWidgets.QMenu.insertSeparator(before)¶
- Parameters
before –
PySide6.QtGui.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’s list of actions before action before
and returns it.
QMenu
takes ownership of the returned QAction
.
See also
- PySide6.QtWidgets.QMenu.isEmpty()¶
- Return type
bool
Returns true
if there are no visible actions inserted into the menu, false otherwise.
See also
- PySide6.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 “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
.
- PySide6.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.
- PySide6.QtWidgets.QMenu.menuAction()¶
- Return type
Returns the action associated with this menu.
- PySide6.QtWidgets.QMenu.popup(pos[, at=None])¶
- Parameters
pos –
PySide6.QtCore.QPoint
Displays the menu so that the action atAction
will be at the specified global position p
. To translate a widget’s local coordinates into global coordinates, use mapToGlobal()
.
When positioning a menu with exec()
or , bear in mind that you cannot rely on the menu’s current size()
. 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, use sizeHint()
which calculates the proper size depending on the menu’s current contents.
See also
- PySide6.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
.
- PySide6.QtWidgets.QMenu.setActiveAction(act)¶
- Parameters
act –
PySide6.QtGui.QAction
Sets the currently highlighted action to act
.
See also
- PySide6.QtWidgets.QMenu.setDefaultAction(arg__1)¶
- Parameters
arg__1 –
PySide6.QtGui.QAction
This sets the default action to act
. The default action may have a visual cue, depending on the current QStyle
. A default action usually indicates what will happen by default when a drop occurs.
See also
- PySide6.QtWidgets.QMenu.setIcon(icon)¶
- Parameters
icon –
PySide6.QtGui.QIcon
This property holds The icon of the menu.
This is equivalent to the icon
property of the menuAction()
.
By default, if no icon is explicitly set, this property contains a null icon.
- PySide6.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
.
- PySide6.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 “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
.
- PySide6.QtWidgets.QMenu.setTitle(title)¶
- Parameters
title – str
This property holds The title of the menu.
This is equivalent to the text
property of the menuAction()
.
By default, this property contains an empty string.
- PySide6.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
.
- PySide6.QtWidgets.QMenu.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.
- PySide6.QtWidgets.QMenu.showTearOffMenu(pos)
- Parameters
pos –
PySide6.QtCore.QPoint
This function will forcibly show the torn off menu making it appear on the user’s desktop at the specified global position pos
.
- PySide6.QtWidgets.QMenu.title()¶
- Return type
str
This property holds The title of the menu.
This is equivalent to the text
property of the menuAction()
.
By default, this property contains an empty string.
- PySide6.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
.
- PySide6.QtWidgets.QMenu.triggered(action)¶
- Parameters
action –
PySide6.QtGui.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.