Table of Contents
- QMenuBar
- Synopsis
- Detailed Description
- Usage
- Platform Dependent Look and Feel
- QMenuBar as a Global Menu Bar
- Examples
PySide2.QtWidgets.QMenuBarPySide2.QtWidgets.QMenuBar.actionAt()PySide2.QtWidgets.QMenuBar.actionGeometry()PySide2.QtWidgets.QMenuBar.activeAction()PySide2.QtWidgets.QMenuBar.addAction()PySide2.QtWidgets.QMenuBar.addMenu()PySide2.QtWidgets.QMenuBar.addSeparator()PySide2.QtWidgets.QMenuBar.clear()PySide2.QtWidgets.QMenuBar.cornerWidget()PySide2.QtWidgets.QMenuBar.hovered()PySide2.QtWidgets.QMenuBar.initStyleOption()PySide2.QtWidgets.QMenuBar.insertMenu()PySide2.QtWidgets.QMenuBar.insertSeparator()PySide2.QtWidgets.QMenuBar.isDefaultUp()PySide2.QtWidgets.QMenuBar.isNativeMenuBar()PySide2.QtWidgets.QMenuBar.setActiveAction()PySide2.QtWidgets.QMenuBar.setCornerWidget()PySide2.QtWidgets.QMenuBar.setDefaultUp()PySide2.QtWidgets.QMenuBar.setNativeMenuBar()PySide2.QtWidgets.QMenuBar.triggered()
Previous topic
Next topic
Quick search
QMenuBar露

Synopsis露
Functions露
def
actionAt(arg__1)def
actionGeometry(arg__1)def
activeAction()def
addAction(arg__1, arg__2)def
addAction(text)def
addAction(text, receiver, member)def
addMenu(icon, title)def
addMenu(menu)def
addMenu(title)def
addSeparator()def
clear()def
cornerWidget([corner=Qt.TopRightCorner])def
initStyleOption(option, action)def
insertMenu(before, menu)def
insertSeparator(before)def
isDefaultUp()def
isNativeMenuBar()def
setActiveAction(action)def
setCornerWidget(w[, corner=Qt.TopRightCorner])def
setDefaultUp(arg__1)def
setNativeMenuBar(nativeMenuBar)
Signals露
Detailed Description露
A menu bar consists of a list of pull-down menu items. You add menu items with
addMenu(). For example, assuming thatmenubaris a pointer to aQMenuBarandfileMenuis a pointer to aQMenu, the following statement inserts the menu into the menu bar:menubar.addMenu(fileMenu)The ampersand in the menu item鈥檚 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
menuBar()function provided inQMainWindow, addingQMenus to the menu bar and addingQActions to the pop-up menus.Example (from the Menus example):
fileMenu = menuBar().addMenu(tr("&File")) fileMenu.addAction(Act)Menu items may be removed with
removeAction().Widgets can be added to menus by using instances of the
QWidgetActionclass to hold them. These actions can then be inserted into menus in the usual way; see theQMenudocumentation 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,
QMenuBaris 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 flagWindow) will be used for the system-wide menu bar.Qt for macOS also provides a menu bar merging feature to make
QMenuBarconform more closely to accepted macOS menu bar layout. The merging functionality is based on string matching the title of aQMenuentry. These strings are translated (usingtr()) in the 鈥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.plistfile (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
quit()You can override this behavior by using the
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
menuBar()to create the shared menu bar, because that menu bar will have theQMainWindowas its parent. That menu bar would only be displayed for the parentQMainWindow.Note
The text used for the application name in the macOS menu bar is obtained from the value set in the
Info.plistfile in the application鈥檚 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鈥檚 menus into the global menu bar, as described.
Examples露
- class PySide2.QtWidgets.QMenuBar([parent=None])露
- param parent:
Constructs a menu bar with parent
parent.
- PySide2.QtWidgets.QMenuBar.actionAt(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtCore.QPoint- Return type:
Returns the
QActionatpt. ReturnsNoneif there is no action atptor if the location has a separator.See also
- PySide2.QtWidgets.QMenuBar.actionGeometry(arg__1)露
- Parameters:
arg__1 鈥
PySide2.QtWidgets.QAction- Return type:
Returns the geometry of action
actas aQRect.See also
- PySide2.QtWidgets.QMenuBar.activeAction()露
- Return type:
Returns the
QActionthat is currently highlighted, if any, elseNone.See also
- PySide2.QtWidgets.QMenuBar.addAction(arg__1, arg__2)露
- Parameters:
arg__1 鈥 str
arg__2 鈥 object
- PySide2.QtWidgets.QMenuBar.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.See also
- PySide2.QtWidgets.QMenuBar.addAction(text, receiver, member)
- Parameters:
text 鈥 str
receiver 鈥
PySide2.QtCore.QObjectmember 鈥 str
- Return type:
- PySide2.QtWidgets.QMenuBar.addMenu(title)露
- Parameters:
title 鈥 str
- Return type:
- PySide2.QtWidgets.QMenuBar.addMenu(icon, title)
- Parameters:
icon 鈥
PySide2.QtGui.QIcontitle 鈥 str
- Return type:
Appends a new
QMenuwithiconandtitleto the menu bar. The menu bar takes ownership of the menu. Returns the new menu.See also
- PySide2.QtWidgets.QMenuBar.addMenu(menu)
- Parameters:
menu 鈥
PySide2.QtWidgets.QMenu- Return type:
Appends
menuto the menu bar. Returns the menu鈥檚 menuAction(). The menu bar does not take ownership of the menu.Note
The returned
QActionobject can be used to hide the corresponding menu.See also
- PySide2.QtWidgets.QMenuBar.addSeparator()露
- Return type:
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 roleon 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.See also
- PySide2.QtWidgets.QMenuBar.cornerWidget([corner=Qt.TopRightCorner])露
- Parameters:
corner 鈥
Corner- Return type:
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
TopRightCornerorTopLeftCornerwill result in a warning.See also
- PySide2.QtWidgets.QMenuBar.hovered(action)露
- Parameters:
action 鈥
PySide2.QtWidgets.QAction
- PySide2.QtWidgets.QMenuBar.initStyleOption(option, action)露
- Parameters:
action 鈥
PySide2.QtWidgets.QAction
Initialize
optionwith the values from the menu bar 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.QMenuBar.insertMenu(before, menu)露
- Parameters:
before 鈥
PySide2.QtWidgets.QActionmenu 鈥
PySide2.QtWidgets.QMenu
- Return type:
This convenience function inserts
menubefore actionbeforeand returns the menus menuAction().See also
- PySide2.QtWidgets.QMenuBar.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 bar鈥檚 list of actions before actionbeforeand returns it.See also
- PySide2.QtWidgets.QMenuBar.isDefaultUp()露
- Return type:
bool
This property holds the popup orientation.
The default popup orientation. By default, menus pop 鈥渄own鈥 the screen. By setting the property to true, the menu will pop 鈥渦p鈥. You might call this for menus that are below the document to which they refer.
If the menu would not fit on the screen, the other direction is used automatically.
- PySide2.QtWidgets.QMenuBar.isNativeMenuBar()露
- Return type:
bool
This property holds Whether or not a menubar will be used as a native menubar on platforms that support it.
This property specifies whether or not the menubar should be used as a native menubar on platforms that support it. The currently supported platforms are macOS, and Linux desktops which use the com.canonical.dbusmenu D-Bus interface (such as Ubuntu Unity). If this property is
true, the menubar is used in the native menubar and is not in the window of its parent; iffalsethe menubar remains in the window. On other platforms, setting this attribute has no effect, and reading this attribute will always returnfalse.The default is to follow whether the
AA_DontUseNativeMenuBarattribute is set for the application. Explicitly setting this property overrides the presence (or absence) of the attribute.
- PySide2.QtWidgets.QMenuBar.setActiveAction(action)露
- Parameters:
action 鈥
PySide2.QtWidgets.QAction
Sets the currently highlighted action to
act.See also
- PySide2.QtWidgets.QMenuBar.setCornerWidget(w[, corner=Qt.TopRightCorner])露
- Parameters:
corner 鈥
Corner
This sets the given
widgetto be shown directly on the left of the first menu item, or on the right of the last menu item, depending oncorner.The menu bar takes ownership of
widget, reparenting it into the menu bar. However, if thecorneralready 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
TopRightCornerorTopLeftCornerwill result in a warning.See also
- PySide2.QtWidgets.QMenuBar.setDefaultUp(arg__1)露
- Parameters:
arg__1 鈥 bool
This property holds the popup orientation.
The default popup orientation. By default, menus pop 鈥渄own鈥 the screen. By setting the property to true, the menu will pop 鈥渦p鈥. You might call this for menus that are below the document to which they refer.
If the menu would not fit on the screen, the other direction is used automatically.
- PySide2.QtWidgets.QMenuBar.setNativeMenuBar(nativeMenuBar)露
- Parameters:
nativeMenuBar 鈥 bool
This property holds Whether or not a menubar will be used as a native menubar on platforms that support it.
This property specifies whether or not the menubar should be used as a native menubar on platforms that support it. The currently supported platforms are macOS, and Linux desktops which use the com.canonical.dbusmenu D-Bus interface (such as Ubuntu Unity). If this property is
true, the menubar is used in the native menubar and is not in the window of its parent; iffalsethe menubar remains in the window. On other platforms, setting this attribute has no effect, and reading this attribute will always returnfalse.The default is to follow whether the
AA_DontUseNativeMenuBarattribute is set for the application. Explicitly setting this property overrides the presence (or absence) of the attribute.
- PySide2.QtWidgets.QMenuBar.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.