QMenuBar

Inheritance diagram of PySide2.QtWidgets.QMenuBar

Synopsis

Functions

Signals

Detailed Description

The PySide2.QtWidgets.QMenuBar class provides a horizontal menu bar.

A menu bar consists of a list of pull-down menu items. You add menu items with PySide2.QtWidgets.QMenuBar.addMenu() . For example, asuming that menubar is a pointer to a PySide2.QtWidgets.QMenuBar and fileMenu is a pointer to a PySide2.QtWidgets.QMenu , the following statement inserts the menu into the menu bar:

menubar.addMenu(fileMenu)

The ampersand in the menu item’s text sets Alt+F as a shortcut for this menu. (You can use “&&” to get a real ampersand in the menu bar.)

There is no need to lay out a menu bar. It automatically sets its own geometry to the top of the parent widget and changes it appropriately whenever the parent is resized.

Usage

In most main window style applications you would use the PySide2.QtWidgets.QMainWindow.menuBar() function provided in PySide2.QtWidgets.QMainWindow , adding PySide2.QtWidgets.QMenu s to the menu bar and adding PySide2.QtWidgets.QAction s to the pop-up menus.

Example (from the Menus example):

fileMenu = menuBar().addMenu(tr("&File"))
fileMenu.addAction(Act)

Menu items may be removed with PySide2.QtWidgets.QWidget.removeAction() .

Widgets can be added to menus by using instances of the PySide2.QtWidgets.QWidgetAction class to hold them. These actions can then be inserted into menus in the usual way; see the PySide2.QtWidgets.QMenu documentation for more details.

Platform Dependent Look and Feel

Different platforms have different requirements for the appearance of menu bars and their behavior when the user interacts with them. For example, Windows systems are often configured so that the underlined character mnemonics that indicate keyboard shortcuts for items in the menu bar are only shown when the Alt key is pressed.

QMenuBar as a Global Menu Bar

On macOS and on certain Linux desktop environments such as Ubuntu Unity, PySide2.QtWidgets.QMenuBar is a wrapper for using the system-wide menu bar. If you have multiple menu bars in one dialog the outermost menu bar (normally inside a widget with widget flag Qt.Window ) will be used for the system-wide menu bar.

Qt for macOS also provides a menu bar merging feature to make PySide2.QtWidgets.QMenuBar conform more closely to accepted macOS menu bar layout. The merging functionality is based on string matching the title of a PySide2.QtWidgets.QMenu entry. These strings are translated (using QObject.tr() ) in the ” PySide2.QtWidgets.QMenuBar ” context. If an entry is moved its slots will still fire as if it was in the original place. The table below outlines the strings looked for and where the entry is placed if matched:

String matches Placement Notes
about.* Application Menu | About <application name> The application name is fetched from the Info.plist file (see note below). If this entry is not found no About item will appear in the Application Menu.
config, options, setup, settings or preferences Application Menu | Preferences If this entry is not found the Settings item will be disabled
quit or exit Application Menu | Quit <application name> If this entry is not found a default Quit item will be created to call QCoreApplication.quit()

You can override this behavior by using the QAction.menuRole() property.

If you want all windows in a Mac application to share one menu bar, you must create a menu bar that does not have a parent. Create a parent-less menu bar this way:

menuBar = QMenuBar()

Note

Do not call QMainWindow.menuBar() to create the shared menu bar, because that menu bar will have the PySide2.QtWidgets.QMainWindow as its parent. That menu bar would only be displayed for the parent PySide2.QtWidgets.QMainWindow .

Note

The text used for the application name in the macOS menu bar is obtained from the value set in the Info.plist file in the application’s bundle. See Qt for macOS - Deployment for more information.

Note

On Linux, if the com.canonical.AppMenu.Registrar service is available on the D-Bus session bus, then Qt will communicate with it to install the application’s menus into the global menu bar, as described.

Examples

The Menus example shows how to use PySide2.QtWidgets.QMenuBar and PySide2.QtWidgets.QMenu . The other main window application examples also provide menus using these classes.

class PySide2.QtWidgets.QMenuBar([parent=nullptr])
Parameters:parentPySide2.QtWidgets.QWidget

Constructs a menu bar with parent parent .

PySide2.QtWidgets.QMenuBar.actionAt(arg__1)
Parameters:arg__1PySide2.QtCore.QPoint
Return type:PySide2.QtWidgets.QAction

Returns the PySide2.QtWidgets.QAction at pt . Returns 0 if there is no action at pt or if the location has a separator.

PySide2.QtWidgets.QMenuBar.actionGeometry(arg__1)
Parameters:arg__1PySide2.QtWidgets.QAction
Return type:PySide2.QtCore.QRect

Returns the geometry of action act as a PySide2.QtCore.QRect .

PySide2.QtWidgets.QMenuBar.activeAction()
Return type:PySide2.QtWidgets.QAction

Returns the PySide2.QtWidgets.QAction that is currently highlighted. A null pointer will be returned if no action is currently selected.

PySide2.QtWidgets.QMenuBar.addAction(text)
Parameters:text – unicode
Return type:PySide2.QtWidgets.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.

PySide2.QtWidgets.QMenuBar.addAction(text, receiver, member)
Parameters:
Return type:

PySide2.QtWidgets.QAction

This is an overloaded function.

This convenience function creates a new action with the given text . The action’s PySide2.QtWidgets.QMenuBar.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.

PySide2.QtWidgets.QMenuBar.addAction(arg__1, arg__2)
Parameters:
  • arg__1 – unicode
  • arg__2PyObject
PySide2.QtWidgets.QMenuBar.addMenu(title)
Parameters:title – unicode
Return type:PySide2.QtWidgets.QMenu

Appends a new PySide2.QtWidgets.QMenu with title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

PySide2.QtWidgets.QMenuBar.addMenu(menu)
Parameters:menuPySide2.QtWidgets.QMenu
Return type:PySide2.QtWidgets.QAction

Appends menu to the menu bar. Returns the menu’s menuAction().

Note

The returned PySide2.QtWidgets.QAction object can be used to hide the corresponding menu.

PySide2.QtWidgets.QMenuBar.addMenu(icon, title)
Parameters:
Return type:

PySide2.QtWidgets.QMenu

Appends a new PySide2.QtWidgets.QMenu with icon and title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.

PySide2.QtWidgets.QMenuBar.addSeparator()
Return type:PySide2.QtWidgets.QAction

Appends a separator to the menu.

PySide2.QtWidgets.QMenuBar.clear()

Removes all the actions from the menu bar.

Note

On macOS , menu items that have been merged to the system menu bar are not removed by this function. One way to handle this would be to remove the extra actions yourself. You can set the menu role on the different menus, so that you know ahead of time which menu items get merged and which do not. Then decide what to recreate or remove yourself.

PySide2.QtWidgets.QMenuBar.cornerWidget([corner=Qt.TopRightCorner])
Parameters:cornerPySide2.QtCore.Qt.Corner
Return type:PySide2.QtWidgets.QWidget

Returns the widget on the left of the first or on the right of the last menu item, depending on corner .

Note

Using a corner other than Qt.TopRightCorner or Qt.TopLeftCorner will result in a warning.

PySide2.QtWidgets.QMenuBar.hovered(action)
Parameters:actionPySide2.QtWidgets.QAction
PySide2.QtWidgets.QMenuBar.initStyleOption(option, action)
Parameters:

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

PySide2.QtWidgets.QMenuBar.insertMenu(before, menu)
Parameters:
Return type:

PySide2.QtWidgets.QAction

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

PySide2.QtWidgets.QMenuBar.insertSeparator(before)
Parameters:beforePySide2.QtWidgets.QAction
Return type:PySide2.QtWidgets.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 bar’s list of actions before action before and returns it.

PySide2.QtWidgets.QMenuBar.isDefaultUp()
Return type:PySide2.QtCore.bool
PySide2.QtWidgets.QMenuBar.isNativeMenuBar()
Return type:PySide2.QtCore.bool
PySide2.QtWidgets.QMenuBar.setActiveAction(action)
Parameters:actionPySide2.QtWidgets.QAction

Sets the currently highlighted action to act .

PySide2.QtWidgets.QMenuBar.setCornerWidget(w[, corner=Qt.TopRightCorner])
Parameters:

This sets the given widget to be shown directly on the left of the first menu item, or on the right of the last menu item, depending on corner .

The menu bar takes ownership of widget , reparenting it into the menu bar. However, if the corner already contains a widget, this previous widget will no longer be managed and will still be a visible child of the menu bar.

Note

Using a corner other than Qt.TopRightCorner or Qt.TopLeftCorner will result in a warning.

PySide2.QtWidgets.QMenuBar.setDefaultUp(arg__1)
Parameters:arg__1PySide2.QtCore.bool
PySide2.QtWidgets.QMenuBar.setNativeMenuBar(nativeMenuBar)
Parameters:nativeMenuBarPySide2.QtCore.bool
PySide2.QtWidgets.QMenuBar.triggered(action)
Parameters:actionPySide2.QtWidgets.QAction