QMenuBar Class▲
-
Header: QMenuBar
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
-
qmake: QT += widgets
-
Inherits: QWidget
-
Group: QMenuBar is part of mainwindow-classes
Detailed Description▲
A menu bar consists of a list of pull-down menu items. You add menu items with addMenu(). For example, assuming that menubar is a pointer to a QMenuBar and fileMenu is a pointer to a QMenu, the following statement inserts the menu into the menu bar:
menubar-&
gt;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 in QMainWindow, adding QMenus to the menu bar and adding QActions to the pop-up menus.
Example (from the Menus example):
fileMenu =
menuBar()-&
gt;addMenu(tr("&File"
));
fileMenu-&
gt;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 the 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▲
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 QObject::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:
String matches |
---|