6.0.0-rc4Source

QMenuBar Class

The QMenuBar class provides a row of menu titles, usually at the top of a window. More…

Header#include <QMenuBar>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQWidget

List of all members, including inherited members

Public Functions

QMenuBar(QWidget *parent = nullptr)
~QMenuBar()
QAction *addAction(const QString &text)
QAction *addAction(const QString &text, Functor functor)
QAction *addMenu(QMenu *menu)
QMenu *addMenu(const QString &title)
QAction *insertMenu(QAction *before, QMenu *menu)
voidclear()
QAction *activeAction() const
voidsetActiveAction(QAction *action)
QAction *actionAt(const QPoint &point) const
QRectactionGeometry(QAction *action) const

Reimplemented Public Functions

Signals

voidtriggered(QAction *action)
voidhovered(QAction *action)

Reimplemented Protected Functions

Detailed Description

A menu bar shows the title of each of its menus. Clicking a title opens the menu below it; while one is open, moving over another title opens that one instead, and Left and Right in a menu go to the one next to it. A title that has no menu is a button that triggers its action. Put the bar at the top of the layout of the window:

QMenuBar *bar = new QMenuBar(window);
QMenu *file = bar->addMenu("&File");
file->addAction("&Open...", [] { openFile(); });
file->addSeparator();
file->addAction("&Quit", [] { QApplication::quit(); });
bar->addMenu("&Edit")->addAction("&Copy");
layout->addWidget(bar);

Alt and the letter after a & in a title opens its menu.

QMenuBar in the light color scheme

See also QMenu and QAction.

Differences from Qt

The bar does not become the menu bar of a window by itself, as the bar of a QMainWindow does: there is no QMainWindow, and no QLayout::setMenuBar(), so the bar is added to a layout like a widget. A bar is not a native menu bar, and there is no extension button for titles that do not fit: they are cut off. F10 and Alt alone do not move the focus to the bar, the titles have no icons, and the bar cannot be setCornerWidget()ed or setDefaultUp().

Member Function Documentation

[explicit] QMenuBar::QMenuBar(QWidget *parent = nullptr)

Constructs a menu bar with the given parent.

[override] QMenuBar::~QMenuBar()

Destroys the menu bar.

QAction * QMenuBar::actionAt(const QPoint &point) const

Returns the action whose title is at point in the bar; nullptr if there is none.

See also actionGeometry().

[override, protected] void QMenuBar::actionEvent(QActionEvent *event)

Reimplements QWidget::actionEvent().

QRect QMenuBar::actionGeometry(QAction *action) const

Returns the rectangle of the title of action in the bar; an empty rectangle if the action is not shown.

See also actionAt().

QAction * QMenuBar::activeAction() const

Returns the action whose title is highlighted; nullptr if none is.

See also setActiveAction().

QAction * QMenuBar::addAction(const QString &text)

Creates an action with the given text, adds it at the end of the bar, and returns it. The bar owns the action. An action with no menu is a title that triggers when it is clicked.

See also addMenu().

template <typename Functor>QAction * QMenuBar::addAction(const QString &text, Functor functor)

Creates an action with the given text, connects its triggered() signal to functor, adds the action at the end of the bar, and returns it.

QAction * QMenuBar::addMenu(QMenu *menu)

Adds menu at the end of the bar, and returns its action. The title of the menu is the text of the title in the bar. The bar does not own the menu.

See also insertMenu().

QMenu * QMenuBar::addMenu(const QString &title)

Creates a menu with the given title, adds it at the end of the bar, and returns it. The bar owns the menu.

See also addMenu(QMenu *).

[override, protected] void QMenuBar::changeEvent(QEvent *event)

Reimplements QWidget::changeEvent().

void QMenuBar::clear()

Removes all the actions of the bar. The actions and the menus that the bar owns are deleted.

[signal] void QMenuBar::hovered(QAction *action)

This signal is emitted when the user moves to an action of the menu bar or of one of its menus, with that action.

QAction * QMenuBar::insertMenu(QAction *before, QMenu *menu)

Adds menu in front of the action before, and returns its action.

See also addMenu().

[override, protected] void QMenuBar::leaveEvent(QEvent *event)

Reimplements QWidget::leaveEvent().

[override] QSize QMenuBar::minimumSizeHint() const

Reimplements QWidget::minimumSizeHint().

[override, protected] void QMenuBar::mouseMoveEvent(QMouseEvent *event)

Reimplements QWidget::mouseMoveEvent().

[override, protected] void QMenuBar::mousePressEvent(QMouseEvent *event)

Reimplements QWidget::mousePressEvent().

[override, protected] void QMenuBar::paintEvent(QPaintEvent *event)

Reimplements QWidget::paintEvent().

void QMenuBar::setActiveAction(QAction *action)

Highlights the title of action, the one that has a menu open or that the mouse is on; nullptr for none. This emits hovered() but does not open the menu.

See also activeAction().

[override] QSize QMenuBar::sizeHint() const

Reimplements QWidget::sizeHint().

[signal] void QMenuBar::triggered(QAction *action)

This signal is emitted when the user triggers an action of the menu bar or of one of its menus, with that action.