QMenu Class | Qt Widgets 5.15.19 (original) (raw)
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus. More...
Member Function Documentation
QMenu::QMenu(const QString &title, QWidget *parent = nullptr)
Constructs a menu with a title and a parent.
Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject).
See also title.
QMenu::QMenu(QWidget *parent = nullptr)
Constructs a menu with parent parent.
Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject).
[signal]
void QMenu::aboutToHide()
This signal is emitted just before the menu is hidden from the user.
This function was introduced in Qt 4.2.
See also aboutToShow() and hide().
[signal]
void QMenu::aboutToShow()
This signal is emitted just before the menu is shown to the user.
See also aboutToHide() and show().
[signal]
void QMenu::hovered(QAction *action)
This signal is emitted when a menu action is highlighted; action is the action that caused the signal to be emitted.
Often this is used to update status information.
See also triggered() and QAction::hovered().
[signal]
void QMenu::triggered(QAction *action)
This signal is emitted when an action in this menu is triggered.
action is the action that caused the signal to be emitted.
Normally, you connect each menu action's triggered() signal to its own custom slot, but sometimes you will want to connect several actions to a single slot, for example, when you have a group of closely related actions, such as "left justify", "center", "right justify".
Note: This signal is emitted for the main parent menu in a hierarchy. Hence, only the parent menu needs to be connected to a slot; sub-menus need not be connected.
See also hovered() and QAction::triggered().
[virtual]
QMenu::~QMenu()
Destroys the menu.
QAction *QMenu::actionAt(const QPoint &pt) const
Returns the item at pt; returns nullptr
if there is no item there.
[override virtual protected]
void QMenu::actionEvent(QActionEvent *e)
Reimplements: QWidget::actionEvent(QActionEvent *event).
QRect QMenu::actionGeometry(QAction *act) const
Returns the geometry of action act.
QAction *QMenu::activeAction() const
Returns the currently highlighted action, or nullptr
if no action is currently highlighted.
See also setActiveAction().
QAction *QMenu::addAction(const QString &text)
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.
QMenu takes ownership of the returned QAction.
See also QWidget::addAction().
QAction *QMenu::addAction(const QIcon &icon, const QString &text)
This is an overloaded function.
This convenience function creates a new action with an icon and some text. The function adds the newly created action to the menu's list of actions, and returns it.
QMenu takes ownership of the returned QAction.
See also QWidget::addAction().
QAction *QMenu::addAction(const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut = 0)
This is an overloaded function.
This convenience function creates a new action with the text text and an optional shortcut shortcut. The action's triggered() signal is connected to the receiver's member slot. The function adds the newly created action to the menu's list of actions and returns it.
QMenu takes ownership of the returned QAction.
See also QWidget::addAction().
QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut = 0)
This is an overloaded function.
This convenience function creates a new action with an icon and some text and an optional shortcut shortcut. The action's triggered() signal is connected to the member slot of the receiver object. The function adds the newly created action to the menu's list of actions, and returns it.
QMenu takes ownership of the returned QAction.
See also QWidget::addAction().
template QAction *QMenu::addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0)
This is an overloaded function.
This convenience function creates a new action with the text text and an optional shortcut shortcut. The action's triggered() signal is connected to the functor. The function adds the newly created action to the menu's list of actions and returns it.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.6.
template QAction *QMenu::addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0)
This is an overloaded function.
This convenience function creates a new action with the text text and an optional shortcut shortcut. The action's triggered() signal is connected to the functor. The functor can be a pointer to a member function of the context object. The newly created action is added to the menu's list of actions and a pointer to it is returned.
If the context object is destroyed, the functor will not be called.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.6.
template QAction *QMenu::addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0)
This is an overloaded function.
This convenience function creates a new action with an icon and some text and an optional shortcut shortcut. The action's triggered() signal is connected to the functor. The function adds the newly created action to the menu's list of actions and returns it.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.6.
template QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0)
This is an overloaded function.
This convenience function creates a new action with an icon and some text and an optional shortcut shortcut. The action's triggered() signal is connected to the functor. The functor can be a pointer to a member function of the context object. The newly created action is added to the menu's list of actions and a pointer to it is returned.
If context is destroyed, the functor will not be called.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.6.
QAction *QMenu::addMenu(QMenu *menu)
This convenience function adds menu as a submenu to this menu. It returns menu's menuAction(). This menu does not take ownership of menu.
See also QWidget::addAction() and QMenu::menuAction().
QMenu *QMenu::addMenu(const QString &title)
Appends a new QMenu with title to the menu. The menu takes ownership of the menu. Returns the new menu.
See also QWidget::addAction() and QMenu::menuAction().
QMenu *QMenu::addMenu(const QIcon &icon, const QString &title)
Appends a new QMenu with icon and title to the menu. The menu takes ownership of the menu. Returns the new menu.
See also QWidget::addAction() and QMenu::menuAction().
QAction *QMenu::addSection(const QString &text)
This convenience function creates a new section action, i.e. an action with QAction::isSeparator() returning true but also having text hint, and adds the new action to this menu's list of actions. It returns the newly created action.
The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.1.
See also QWidget::addAction().
QAction *QMenu::addSection(const QIcon &icon, const QString &text)
This convenience function creates a new section action, i.e. an action with QAction::isSeparator() returning true but also having text and icon hints, and adds the new action to this menu's list of actions. It returns the newly created action.
The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.1.
See also QWidget::addAction().
QAction *QMenu::addSeparator()
This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true, and adds the new action to this menu's list of actions. It returns the newly created action.
QMenu takes ownership of the returned QAction.
See also QWidget::addAction().
[override virtual protected]
void QMenu::changeEvent(QEvent *e)
Reimplements: QWidget::changeEvent(QEvent *event).
void QMenu::clear()
Removes all the menu's actions. Actions owned by the menu and not shown in any other widget are deleted.
See also removeAction().
[protected]
int QMenu::columnCount() const
If a menu does not fit on the screen it lays itself out so that it does fit. It is style dependent what layout means (for example, on Windows it will use multiple columns).
This functions returns the number of columns necessary.
QAction *QMenu::defaultAction() const
Returns the current default action.
See also setDefaultAction().
[override virtual protected]
void QMenu::enterEvent(QEvent *)
Reimplements: QWidget::enterEvent(QEvent *event).
[override virtual protected]
bool QMenu::event(QEvent *e)
Reimplements: QWidget::event(QEvent *event).
QAction *QMenu::exec()
Executes this menu synchronously.
This is equivalent to exec(pos())
.
This returns the triggered QAction in either the popup menu or one of its submenus, or nullptr
if no item was triggered (normally because the user pressed Esc).
In most situations you'll want to specify the position yourself, for example, the current mouse position:
or aligned to a widget:
exec(somewidget.mapToGlobal(QPoint(0,0)));
or in reaction to a QMouseEvent *e:
QAction *QMenu::exec(const QPoint &p, QAction *action = nullptr)
This is an overloaded function.
Executes this menu synchronously.
Pops up the menu so that the action action will be at the specified global position p. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().
This returns the triggered QAction in either the popup menu or one of its submenus, or nullptr
if no item was triggered (normally because the user pressed Esc).
Note that all signals are emitted as usual. If you connect a QAction to a slot and call the menu's exec(), you get the result both via the signal-slot connection and in the return value of exec().
Common usage is to position the menu at the current mouse position:
or aligned to a widget:
exec(somewidget.mapToGlobal(QPoint(0, 0)));
or in reaction to a QMouseEvent *e:
When positioning a menu with exec() or popup(), bear in mind that you cannot rely on the menu's current size(). For performance reasons, the menu adapts its size only when necessary. So in many cases, the size before and after the show is different. Instead, use sizeHint() which calculates the proper size depending on the menu's current contents.
See also popup() and QWidget::mapToGlobal().
[static]
QAction *QMenu::exec(QList<QAction *> actions, const QPoint &pos, QAction *at = nullptr, QWidget *parent = nullptr)
This is an overloaded function.
Executes a menu synchronously.
The menu's actions are specified by the list of actions. The menu will pop up so that the specified action, at, appears at global position pos. If at is not specified then the menu appears at position pos. parent is the menu's parent widget; specifying the parent will provide context when pos alone is not enough to decide where the menu should go (e.g., with multiple desktops or when the parent is embedded in QGraphicsView).
The function returns the triggered QAction in either the popup menu or one of its submenus, or nullptr
if no item was triggered (normally because the user pressed Esc).
This is equivalent to:
QMenu menu; QAction *at = actions[0]; // Assumes actions is not empty for (QAction *a : qAsConst(actions)) menu.addAction(a); menu.exec(pos, at);
See also popup() and QWidget::mapToGlobal().
[override virtual protected]
bool QMenu::focusNextPrevChild(bool next)
Reimplements: QWidget::focusNextPrevChild(bool next).
[override virtual protected]
void QMenu::hideEvent(QHideEvent *)
Reimplements: QWidget::hideEvent(QHideEvent *event).
void QMenu::hideTearOffMenu()
This function will forcibly hide the torn off menu making it disappear from the user's desktop.
See also showTearOffMenu(), isTearOffMenuVisible(), and isTearOffEnabled().
[protected]
void QMenu::initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
Initialize option with the values from this menu 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.
See also QStyleOption::initFrom() and QMenuBar::initStyleOption().
QAction *QMenu::insertMenu(QAction *before, QMenu *menu)
This convenience function inserts menu before action before and returns the menus menuAction().
See also QWidget::insertAction() and addMenu().
QAction *QMenu::insertSection(QAction *before, const QString &text)
This convenience function creates a new title action, i.e. an action with QAction::isSeparator() returning true but also having text hint. The function inserts the newly created action into this menu's list of actions before action before and returns it.
The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.1.
See also QWidget::insertAction() and addSection().
QAction *QMenu::insertSection(QAction *before, const QIcon &icon, const QString &text)
This convenience function creates a new title action, i.e. an action with QAction::isSeparator() returning true but also having text and icon hints. The function inserts the newly created action into this menu's list of actions before action before and returns it.
The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.
QMenu takes ownership of the returned QAction.
This function was introduced in Qt 5.1.
See also QWidget::insertAction() and addSection().
QAction *QMenu::insertSeparator(QAction *before)
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's list of actions before action before and returns it.
QMenu takes ownership of the returned QAction.
See also QWidget::insertAction() and addSeparator().
bool QMenu::isEmpty() const
Returns true
if there are no visible actions inserted into the menu, false otherwise.
This function was introduced in Qt 4.2.
See also QWidget::actions().
bool QMenu::isTearOffMenuVisible() const
When a menu is torn off a second menu is shown to display the menu contents in a new window. When the menu is in this mode and the menu is visible returns true
; otherwise false.
See also showTearOffMenu(), hideTearOffMenu(), and isTearOffEnabled().
[override virtual protected]
void QMenu::keyPressEvent(QKeyEvent *e)
Reimplements: QWidget::keyPressEvent(QKeyEvent *event).
[override virtual protected]
void QMenu::leaveEvent(QEvent *)
Reimplements: QWidget::leaveEvent(QEvent *event).
QAction *QMenu::menuAction() const
Returns the action associated with this menu.
[override virtual protected]
void QMenu::mouseMoveEvent(QMouseEvent *e)
Reimplements: QWidget::mouseMoveEvent(QMouseEvent *event).
[override virtual protected]
void QMenu::mousePressEvent(QMouseEvent *e)
Reimplements: QWidget::mousePressEvent(QMouseEvent *event).
[override virtual protected]
void QMenu::mouseReleaseEvent(QMouseEvent *e)
Reimplements: QWidget::mouseReleaseEvent(QMouseEvent *event).
[override virtual protected]
void QMenu::paintEvent(QPaintEvent *e)
Reimplements: QWidget::paintEvent(QPaintEvent *event).
void QMenu::popup(const QPoint &p, QAction *atAction = nullptr)
Displays the menu so that the action atAction will be at the specified global position p. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().
When positioning a menu with exec() or popup(), bear in mind that you cannot rely on the menu's current size(). For performance reasons, the menu adapts its size only when necessary, so in many cases, the size before and after the show is different. Instead, use sizeHint() which calculates the proper size depending on the menu's current contents.
See also QWidget::mapToGlobal() and exec().
void QMenu::setActiveAction(QAction *act)
Sets the currently highlighted action to act.
See also activeAction().
void QMenu::setAsDockMenu()
Set this menu to be the dock menu available by option-clicking on the application dock icon. Available on macOS only.
This function was introduced in Qt 5.2.
void QMenu::setDefaultAction(QAction *act)
This sets the default action to act. The default action may have a visual cue, depending on the current QStyle. A default action usually indicates what will happen by default when a drop occurs.
See also defaultAction().
void QMenu::showTearOffMenu(const QPoint &pos)
This function will forcibly show the torn off menu making it appear on the user's desktop at the specified global position pos.
This function was introduced in Qt 5.7.
See also hideTearOffMenu(), isTearOffMenuVisible(), and isTearOffEnabled().
void QMenu::showTearOffMenu()
This is an overloaded function.
This function will forcibly show the torn off menu making it appear on the user's desktop under the mouse currsor.
This function was introduced in Qt 5.7.
See also hideTearOffMenu(), isTearOffMenuVisible(), and isTearOffEnabled().
[override virtual]
QSize QMenu::sizeHint() const
Reimplements an access function for property: QWidget::sizeHint.
[override virtual protected]
void QMenu::timerEvent(QTimerEvent *e)
Reimplements: QObject::timerEvent(QTimerEvent *event).
NSMenu *QMenu::toNSMenu()
Returns the native NSMenu for this menu. Available on macOS only.
Note: Qt sets the delegate on the native menu. If you need to set your own delegate, make sure you save the original one and forward any calls to it.
This function was introduced in Qt 5.2.
[override virtual protected]
void QMenu::wheelEvent(QWheelEvent *e)
Reimplements: QWidget::wheelEvent(QWheelEvent *event).