QMenu

Synopsis

Functions

Signals

Static functions

  • def exec_ (actions, pos, at, parent)
  • def exec_ (actions, pos[, at=None])

Detailed Description

The PySide.QtGui.QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.

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 QMenuBar.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 PySide.QtGui.QMenu.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.

../../_images/plastique-menu.png ../../_images/windowsxp-menu.png ../../_images/macintosh-menu.png

Fig. A menu shown in Plastique widget style , Windows XP widget style , and Macintosh widget style .

Actions

A menu consists of a list of action items. Actions are added with the PySide.QtGui.QMenu.addAction() , PySide.QtGui.QWidget.addActions() and PySide.QtGui.QWidget.insertAction() functions. An action is represented vertically and rendered by PySide.QtGui.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 PySide.QtGui.QWidget.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 PySide.QtGui.QMenu.addSeparator() , submenus with PySide.QtGui.QMenu.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 notifed whenever the item is PySide.QtGui.QAction.triggered() . In addition, PySide.QtGui.QMenu provides two signals, activated() and highlighted() , which signal the PySide.QtGui.QAction that was triggered from the menu.

You clear a menu with PySide.QtGui.QMenu.clear() and remove individual action items with PySide.QtGui.QWidget.removeAction() .

A PySide.QtGui.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 PySide.QtGui.QMenu.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 PySide.QtGui.QToolBar instead.

Widgets can be inserted into menus with the PySide.QtGui.QWidgetAction class. Instances of this class are used to hold widgets, and are inserted into menus with the PySide.QtGui.QMenu.addAction() overload that takes a PySide.QtGui.QAction .

Conversely, actions can be added to widgets with the PySide.QtGui.QMenu.addAction() , PySide.QtGui.QWidget.addActions() and PySide.QtGui.QWidget.insertAction() functions.

Warning

To make PySide.QtGui.QMenu visible on the screen, exec() or PySide.QtGui.QMenu.popup() should be used instead of PySide.QtGui.QWidget.show() .

QMenu on Qt for Windows CE

If a menu is integrated into the native menubar on Windows Mobile we do not support the signals: aboutToHide (), aboutToShow () and hovered (). It is not possible to display an icon in a native menu on Windows Mobile.

QMenu on Mac OS X with Qt build against Cocoa

PySide.QtGui.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 PySide.QtGui.QMenuBar and PySide.QtGui.QMenu in your application.

Important inherited functions:PySide.QtGui.QMenu.addAction() , PySide.QtGui.QWidget.removeAction() , PySide.QtGui.QMenu.clear() , PySide.QtGui.QMenu.addSeparator() , and PySide.QtGui.QMenu.addMenu() .

See also

PySide.QtGui.QMenuBar GUI Design Handbook: Menu, Drop-Down and Pop-Up Application Example Menus Example Recent Files Example

class PySide.QtGui.QMenu([parent=None])
class PySide.QtGui.QMenu(title[, parent=None])
Parameters:

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 PySide.QtCore.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 PySide.QtCore.QObject ).

PySide.QtGui.QMenu.aboutToHide()
PySide.QtGui.QMenu.aboutToShow()
PySide.QtGui.QMenu.actionAt(arg__1)
Parameters:arg__1PySide.QtCore.QPoint
Return type:PySide.QtGui.QAction

Returns the item at pt ; returns 0 if there is no item there.

PySide.QtGui.QMenu.actionGeometry(arg__1)
Parameters:arg__1PySide.QtGui.QAction
Return type:PySide.QtCore.QRect

Returns the geometry of action act .

PySide.QtGui.QMenu.activeAction()
Return type:PySide.QtGui.QAction

Returns the currently highlighted action, or 0 if no action is currently highlighted.

PySide.QtGui.QMenu.addAction(icon, text, receiver, member[, shortcut=0])
Parameters:
Return type:

PySide.QtGui.QAction

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 PySide.QtGui.QAction.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.

PySide.QtGui.QMenu.addAction(text)
Parameters:text – unicode
Return type:PySide.QtGui.QAction

This is an overloaded function.

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.

PySide.QtGui.QMenu.addAction(icon, text)
Parameters:
Return type:

PySide.QtGui.QAction

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.

PySide.QtGui.QMenu.addAction(arg__1, arg__2[, arg__3=0])
Parameters:
PySide.QtGui.QMenu.addAction(arg__1, arg__2, arg__3[, arg__4=0])
Parameters:
PySide.QtGui.QMenu.addAction(text, receiver, member[, shortcut=0])
Parameters:
Return type:

PySide.QtGui.QAction

This is an overloaded function.

This convenience function creates a new action with the text text and an optional shortcut shortcut . The action’s PySide.QtGui.QAction.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.

PySide.QtGui.QMenu.addMenu(icon, title)
Parameters:
Return type:

PySide.QtGui.QMenu

Appends a new PySide.QtGui.QMenu with icon and title to the menu. The menu takes ownership of the menu. Returns the new menu.

PySide.QtGui.QMenu.addMenu(title)
Parameters:title – unicode
Return type:PySide.QtGui.QMenu

Appends a new PySide.QtGui.QMenu with title to the menu. The menu takes ownership of the menu. Returns the new menu.

PySide.QtGui.QMenu.addMenu(menu)
Parameters:menuPySide.QtGui.QMenu
Return type:PySide.QtGui.QAction

This convenience function adds menu as a submenu to this menu. It returns menu ‘s PySide.QtGui.QMenu.menuAction() . This menu does not take ownership of menu .

PySide.QtGui.QMenu.addSeparator()
Return type:PySide.QtGui.QAction

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.

PySide.QtGui.QMenu.clear()

Removes all the menu’s actions. Actions owned by the menu and not shown in any other widget are deleted.

PySide.QtGui.QMenu.columnCount()
Return type:PySide.QtCore.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.

PySide.QtGui.QMenu.defaultAction()
Return type:PySide.QtGui.QAction

Returns the current default action.

static PySide.QtGui.QMenu.exec_(actions, pos, at, parent)
Parameters:
Return type:

PySide.QtGui.QAction

PySide.QtGui.QMenu.exec_(pos[, at=None])
Parameters:
Return type:

PySide.QtGui.QAction

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 QWidget.mapToGlobal() .

This returns the triggered PySide.QtGui.QAction in either the popup menu or one of its submenus, or 0 if no item was triggered (normally because the user pressed Esc).

Note that all signals are emitted as usual. If you connect a PySide.QtGui.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 PySide.QtGui.QMouseEvent *e:

exec_(e.globalPos())

When positioning a menu with exec() or PySide.QtGui.QMenu.popup() , bear in mind that you cannot rely on the menu’s current PySide.QtGui.QWidget.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 PySide.QtGui.QMenu.sizeHint() which calculates the proper size depending on the menu’s current contents.

static PySide.QtGui.QMenu.exec_(actions, pos[, at=None])
Parameters:
Return type:

PySide.QtGui.QAction

PySide.QtGui.QMenu.exec_()
Return type:PySide.QtGui.QAction

Executes this menu synchronously.

This is equivalent to exec(pos()) .

This returns the triggered PySide.QtGui.QAction in either the popup menu or one of its submenus, or 0 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 PySide.QtGui.QMouseEvent *e:

exec_(e.globalPos())
PySide.QtGui.QMenu.hideTearOffMenu()

This function will forcibly hide the torn off menu making it disappear from the users desktop.

PySide.QtGui.QMenu.hovered(action)
Parameters:actionPySide.QtGui.QAction
PySide.QtGui.QMenu.icon()
Return type:PySide.QtGui.QIcon

This property holds The icon of the menu.

This is equivalent to the QAction.icon property of the PySide.QtGui.QMenu.menuAction() .

By default, if no icon is explicitly set, this property contains a null icon.

PySide.QtGui.QMenu.initStyleOption(option, action)
Parameters:

Initialize option with the values from this menu and information from action . This method is useful for subclasses when they need a PySide.QtGui.QStyleOptionMenuItem , but don’t want to fill in all the information themselves.

PySide.QtGui.QMenu.insertMenu(before, menu)
Parameters:
Return type:

PySide.QtGui.QAction

This convenience function inserts menu before action before and returns the menus PySide.QtGui.QMenu.menuAction() .

PySide.QtGui.QMenu.insertSeparator(before)
Parameters:beforePySide.QtGui.QAction
Return type:PySide.QtGui.QAction

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.

PySide.QtGui.QMenu.internalDelayedPopup()
PySide.QtGui.QMenu.internalSetSloppyAction()
PySide.QtGui.QMenu.isEmpty()
Return type:PySide.QtCore.bool

Returns true if there are no visible actions inserted into the menu, false otherwise.

PySide.QtGui.QMenu.isTearOffEnabled()
Return type:PySide.QtCore.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.

PySide.QtGui.QMenu.isTearOffMenuVisible()
Return type:PySide.QtCore.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.

PySide.QtGui.QMenu.menuAction()
Return type:PySide.QtGui.QAction

Returns the action associated with this menu.

PySide.QtGui.QMenu.popup(pos[, at=None])
Parameters:

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 QWidget.mapToGlobal() .

When positioning a menu with exec() or PySide.QtGui.QMenu.popup() , bear in mind that you cannot rely on the menu’s current PySide.QtGui.QWidget.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 PySide.QtGui.QMenu.sizeHint() which calculates the proper size depending on the menu’s current contents.

See also

QWidget.mapToGlobal() exec()

PySide.QtGui.QMenu.separatorsCollapsible()
Return type:PySide.QtCore.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.

PySide.QtGui.QMenu.setActiveAction(act)
Parameters:actPySide.QtGui.QAction

Sets the currently highlighted action to act .

PySide.QtGui.QMenu.setDefaultAction(arg__1)
Parameters:arg__1PySide.QtGui.QAction

This sets the default action to act . The default action may have a visual cue, depending on the current PySide.QtGui.QStyle . A default action usually indicates what will happen by default when a drop occurs.

PySide.QtGui.QMenu.setIcon(icon)
Parameters:iconPySide.QtGui.QIcon

This property holds The icon of the menu.

This is equivalent to the QAction.icon property of the PySide.QtGui.QMenu.menuAction() .

By default, if no icon is explicitly set, this property contains a null icon.

PySide.QtGui.QMenu.setSeparatorsCollapsible(collapse)
Parameters:collapsePySide.QtCore.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.

PySide.QtGui.QMenu.setTearOffEnabled(arg__1)
Parameters:arg__1PySide.QtCore.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.

PySide.QtGui.QMenu.setTitle(title)
Parameters:title – unicode

This property holds The title of the menu.

This is equivalent to the QAction.text property of the PySide.QtGui.QMenu.menuAction() .

By default, this property contains an empty string.

PySide.QtGui.QMenu.title()
Return type:unicode

This property holds The title of the menu.

This is equivalent to the QAction.text property of the PySide.QtGui.QMenu.menuAction() .

By default, this property contains an empty string.

PySide.QtGui.QMenu.triggered(action)
Parameters:actionPySide.QtGui.QAction