addAction(text, receiver, member)
A menu bar consists of a list of pull-down menu items. You add menu items with
addMenu() . For example, asuming that
menubar is a pointer to a
fileMenu is a pointer to a
QMenu , the following statement inserts the menu into the menu bar:
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.
Example (from the Menus example):
fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(newAct);
Menu items may be removed with
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,
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
Window ) will be used for the system-wide menu bar.
Qt for macOS also provides a menu bar merging feature to make
QMenuBar conform more closely to accepted macOS menu bar layout. The merging functionality is based on string matching the title of a
QMenu entry. These strings are translated (using
tr() ) 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:
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
You can override this behavior by using the
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(None)
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.
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.
Constructs a menu bar with parent
None if there is no action at
pt or if the location has a separator.
Returns the geometry of action
act as a
QAction that is currently highlighted, if any, else
- PySide6.QtWidgets.QMenuBar.addAction(arg__1, arg__2)¶
arg__1 – str
arg__2 – object
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.
- PySide6.QtWidgets.QMenuBar.addAction(text, receiver, member)
This is an overloaded function.
This convenience function creates a new action with the given
text. The action’s signal is connected to the
member slot. The function adds the newly created action to the menu’s list of actions and returns it.
title – str
- Return type
- PySide6.QtWidgets.QMenuBar.addMenu(icon, title)
Appends a new
title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.
menu to the menu bar. Returns the menu’s menuAction(). The menu bar does not take ownership of the menu.
QAction object can be used to hide the corresponding menu.
Appends a separator to the menu.
Removes all the actions from the menu bar.
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.
Returns the widget on the left of the first or on the right of the last menu item, depending on
Using a corner other than
TopLeftCorner will result in a warning.
- PySide6.QtWidgets.QMenuBar.initStyleOption(option, action)¶
option with the values from the menu bar 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.
- PySide6.QtWidgets.QMenuBar.insertMenu(before, menu)¶
This convenience function inserts
menu before action
before and returns the menus menuAction().
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’s list of actions before action
before and returns it.
- Return type
- Return type
Sets the currently highlighted action to
- PySide6.QtWidgets.QMenuBar.setCornerWidget(w[, corner=Qt.TopRightCorner])¶
nativeMenuBar – bool
© 2021 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.