- 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.