PySide6.QtWidgets.QMenuBar¶
- class QMenuBar¶
The
QMenuBar
class provides a horizontal menu bar. More…Synopsis¶
Properties¶
defaultUpᅟ
- The popup orientationnativeMenuBarᅟ
- Whether or not a menubar will be used as a native menubar on platforms that support it
Methods¶
def
__init__()
def
actionAt()
def
actionGeometry()
def
activeAction()
def
addMenu()
def
addSeparator()
def
clear()
def
cornerWidget()
def
insertMenu()
def
isDefaultUp()
def
setDefaultUp()
Virtual methods¶
Signals¶
def
hovered()
def
triggered()
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¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
A menu bar consists of a list of pull-down menu items. You add menu items with
addMenu()
. For example, assuming thatmenubar
is a pointer to aQMenuBar
andfileMenu
is a pointer to aQMenu
, 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
menuBar()
function provided inQMainWindow
, addingQMenu
s to the menu bar and adding QActions to the pop-up menus.Example (from the Menus example):
fileMenu = menuBar().addMenu(tr("File")) fileMenu.addAction(newAct)
Menu items may be removed with
removeAction()
.Widgets can be added to menus by using instances of the
QWidgetAction
class to hold them. These actions can then be inserted into menus in the usual way; see theQMenu
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,
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
QMenuBar
conform more closely to accepted macOS menu bar layout. If an entry is moved its slots will still fire as if it was in the original place.The merging functionality is based on the QAction::menuRole() of the menu entries. If an item has QAction::TextHeuristicRole, the role is determined by string matching the title using the following heuristics:
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 setting the QAction::menuRole() property to QAction::NoRole.
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)
Note
Do not call
menuBar()
to create the shared menu bar, because that menu bar will have theQMainWindow
as 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.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
QMenuBar
andQMenu
. The other main window application examples also provide menus using these classes.See also
QMenu
Introduction to Apple Human Interface Guidelines Menus ExampleNote
Properties can be used directly when
from __feature__ import true_property
is used or via accessor functions otherwise.- property defaultUpᅟ: bool¶
This property holds the popup orientation.
The default popup orientation. By default, menus pop “down” the screen. By setting the property to true, the menu will pop “up”. 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.
- Access functions:
- property 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; iffalse
the 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 Qt::AA_DontUseNativeMenuBar attribute is set for the application. Explicitly setting this property overrides the presence (or absence) of the attribute.
- Access functions:
Constructs a menu bar with parent
parent
.Returns the QAction at
pt
. ReturnsNone
if there is no action atpt
or if the location has a separator.See also
Returns the geometry of action
act
as a QRect.See also
Returns the QAction that is currently highlighted, if any, else
None
.See also
Appends
menu
to the menu bar. Returns the menu’s menuAction(). The menu bar does not take ownership of the menu.Note
The returned QAction object can be used to hide the corresponding menu.
See also
- addMenu(title)
- Parameters:
title – str
- Return type:
Appends a new
QMenu
withtitle
to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.See also
Appends a new
QMenu
withicon
andtitle
to the menu bar. The menu bar takes ownership of the menu. Returns the new menu.See also
Appends a separator to the menu.
- 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.
See also
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.
See also
This signal is emitted when a menu action is highlighted;
action
is the action that caused the event to be sent.Often this is used to update status information.
See also
- initStyleOption(option, action)¶
- Parameters:
option –
QStyleOptionMenuItem
action –
QAction
Initialize
option
with the values from the menu bar and information fromaction
. This method is useful for subclasses when they need aQStyleOptionMenuItem
, but don’t want to fill in all the information themselves.See also
This convenience function inserts
menu
before actionbefore
and returns the menus menuAction().See also
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.See also
- isDefaultUp()¶
- Return type:
bool
Getter of property
defaultUpᅟ
.- isNativeMenuBar()¶
- Return type:
bool
Getter of property
nativeMenuBarᅟ
.Sets the currently highlighted action to
act
.See also
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 oncorner
.The menu bar takes ownership of
widget
, reparenting it into the menu bar. However, if thecorner
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.
See also
- setDefaultUp(arg__1)¶
- Parameters:
arg__1 – bool
See also
Setter of property
defaultUpᅟ
.- setNativeMenuBar(nativeMenuBar)¶
- Parameters:
nativeMenuBar – bool
See also
Setter of property
nativeMenuBarᅟ
.This signal is emitted when an action in a menu belonging to this menubar is triggered as a result of a mouse click;
action
is the action that caused the signal to be emitted.Normally, you connect each menu action to a single slot using QAction::triggered(), but sometimes you will want to connect several items to a single slot (most often if the user selects from an array). This signal is useful in such cases.
See also