CopperSpice API  1.9.2
QMenu Class Reference

Provides a menu widget for use in menu bars, context menus, and other popup menus. More...

Inheritance diagram for QMenu:
QWidget QObject QPaintDevice

Public Signals

void aboutToHide ()
 
void aboutToShow ()
 
void hovered (QAction *action)
 
void triggered (QAction *action)
 
- Public Signals inherited from QWidget
void customContextMenuRequested (const QPoint &pos)
 
void windowIconChanged (const QIcon &icon)
 
void windowIconTextChanged (const QString &iconText)
 
void windowTitleChanged (const QString &title)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Methods

 QMenu (const QString &title, QWidget *parent=nullptr)
 
 QMenu (QWidget *parent=nullptr)
 
 ~QMenu ()
 
QActionactionAt (const QPoint &point) const
 
QRect actionGeometry (QAction *action) const
 
QActionactiveAction () const
 
QActionaddAction (const QIcon &icon, const QString &text)
 
QActionaddAction (const QIcon &icon, const QString &text, const QObject *receiver, const QString &member, const QKeySequence &shortcut=0)
 
template<typename Func1 >
QActionaddAction (const QIcon &icon, const QString &text, Func1 slot, const QKeySequence &shortcut=QKeySequence ())
 
QActionaddAction (const QString &text)
 
QActionaddAction (const QString &text, const QObject *receiver, const QString &member, const QKeySequence &shortcut=QKeySequence ())
 
void addAction (QAction *action)
 
QMenu * addMenu (const QIcon &icon, const QString &title)
 
QMenu * addMenu (const QString &title)
 
QActionaddMenu (QMenu *menu)
 
QActionaddSection (const QIcon &icon, const QString &text)
 
QActionaddSection (const QString &text)
 
QActionaddSeparator ()
 
void clear ()
 
QActiondefaultAction () const
 
QActionexec ()
 
QActionexec (const QPoint &point, QAction *action=nullptr)
 
void hideTearOffMenu ()
 
QIcon icon () const
 
QActioninsertMenu (QAction *action, QMenu *menu)
 
QActioninsertSection (QAction *location, const QIcon &icon, const QString &text)
 
QActioninsertSection (QAction *location, const QString &text)
 
QActioninsertSeparator (QAction *action)
 
bool isEmpty () const
 
bool isTearOffEnabled () const
 
bool isTearOffMenuVisible () const
 
QActionmenuAction () const
 
void popup (const QPoint &point, QAction *action=nullptr)
 
bool separatorsCollapsible () const
 
void setActiveAction (QAction *action)
 
void setAsDockMenu ()
 
void setDefaultAction (QAction *action)
 
void setIcon (const QIcon &icon)
 
void setSeparatorsCollapsible (bool collapse)
 
void setTearOffEnabled (bool enabled)
 
void setTitle (const QString &title)
 
void setToolTipsVisible (bool visible)
 
QSize sizeHint () const override
 
QString title () const
 
NSMenu * toNSMenu ()
 
bool toolTipsVisible () const
 
- Public Methods inherited from QWidget
 QWidget (QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::EmptyFlag)
 
 ~QWidget ()
 
bool acceptDrops () const
 
QString accessibleDescription () const
 
QString accessibleName () const
 
QList< QAction * > actions () const
 
void activateWindow ()
 
void addAction (QAction *action)
 
void addActions (const QList< QAction * > &actions)
 
void adjustSize ()
 
bool autoFillBackground () const
 
QPalette::ColorRole backgroundRole () const
 
QBackingStorebackingStore () const
 
QSize baseSize () const
 
QWidget * childAt (const QPoint &position) const
 
QWidget * childAt (int x, int y) const
 
QRect childrenRect () const
 
QRegion childrenRegion () const
 
void clearFocus ()
 
void clearMask ()
 
QMargins contentsMargins () const
 
QRect contentsRect () const
 
Qt::ContextMenuPolicy contextMenuPolicy () const
 
QCursor cursor () const
 
WId effectiveWinId () const
 
void ensurePolished () const
 
Qt::FocusPolicy focusPolicy () const
 
QWidget * focusProxy () const
 
QWidget * focusWidget () const
 
const QFontfont () const
 
QFontInfo fontInfo () const
 
QFontMetrics fontMetrics () const
 
QPalette::ColorRole foregroundRole () const
 
QRect frameGeometry () const
 
QSize frameSize () const
 
const QRectgeometry () const
 
void getContentsMargins (int *left, int *top, int *right, int *bottom) const
 
QPixmap grab (const QRect &rectangle=QRect (QPoint (0, 0), QSize (-1,-1)))
 
void grabGesture (Qt::GestureType gestureType, Qt::GestureFlags flags=Qt::GestureFlags ())
 
void grabKeyboard ()
 
void grabMouse ()
 
void grabMouse (const QCursor &cursor)
 
int grabShortcut (const QKeySequence &key, Qt::ShortcutContext context=Qt::WindowShortcut)
 
QGraphicsEffectgraphicsEffect () const
 
QGraphicsProxyWidgetgraphicsProxyWidget () const
 
bool hasEditFocus () const
 
bool hasFocus () const
 
virtual bool hasHeightForWidth () const
 
bool hasMouseTracking () const
 
int height () const
 
virtual int heightForWidth (int width) const
 
Qt::InputMethodHints inputMethodHints () const
 
virtual QVariant inputMethodQuery (Qt::InputMethodQuery query) const
 
void insertAction (QAction *before, QAction *action)
 
void insertActions (QAction *before, QList< QAction * > actions)
 
bool isActiveWindow () const
 
bool isAncestorOf (const QWidget *child) const
 
bool isEnabled () const
 
bool isEnabledTo (const QWidget *parent) const
 
bool isEnabledToTLW () const
 
bool isFullScreen () const
 
bool isHidden () const
 
bool isMaximized () const
 
bool isMinimized () const
 
bool isModal () const
 
bool isTopLevel () const
 
bool isVisible () const
 
bool isVisibleTo (const QWidget *parent) const
 
bool isWindow () const
 
bool isWindowModified () const
 
QLayoutlayout () const
 
Qt::LayoutDirection layoutDirection () const
 
QLocale locale () const
 
QPoint mapFrom (const QWidget *parent, const QPoint &pos) const
 
QPoint mapFromGlobal (const QPoint &pos) const
 
QPoint mapFromParent (const QPoint &pos) const
 
QPoint mapTo (const QWidget *parent, const QPoint &pos) const
 
QPoint mapToGlobal (const QPoint &pos) const
 
QPoint mapToParent (const QPoint &pos) const
 
QRegion mask () const
 
int maximumHeight () const
 
QSize maximumSize () const
 
int maximumWidth () const
 
int minimumHeight () const
 
QSize minimumSize () const
 
virtual QSize minimumSizeHint () const
 
int minimumWidth () const
 
void move (const QPoint &point)
 
void move (int x, int y)
 
QWidget * nativeParentWidget () const
 
QWidget * nextInFocusChain () const
 
QRect normalGeometry () const
 
void overrideWindowFlags (Qt::WindowFlags flags)
 
QPaintEnginepaintEngine () const override
 
const QPalettepalette () const
 
QWidget * parentWidget () const
 
QPoint pos () const
 
QWidget * previousInFocusChain () const
 
QRect rect () const
 
void releaseKeyboard ()
 
void releaseMouse ()
 
void releaseShortcut (int id)
 
void removeAction (QAction *action)
 
void render (QPaintDevice *target, const QPoint &targetOffset=QPoint (), const QRegion &sourceRegion=QRegion (), RenderFlags renderFlags=RenderFlags (DrawWindowBackground|DrawChildren))
 
void render (QPainter *painter, const QPoint &targetOffset=QPoint (), const QRegion &sourceRegion=QRegion (), RenderFlags renderFlags=RenderFlags (DrawWindowBackground|DrawChildren))
 
void repaint (const QRect &rect)
 
void repaint (const QRegion &region)
 
void repaint (int x, int y, int w, int h)
 
void resize (const QSize &size)
 
void resize (int w, int h)
 
bool restoreGeometry (const QByteArray &geometry)
 
QByteArray saveGeometry () const
 
void scroll (int dx, int dy)
 
void scroll (int dx, int dy, const QRect &rect)
 
void setAcceptDrops (bool on)
 
void setAccessibleDescription (const QString &description)
 
void setAccessibleName (const QString &name)
 
void setAttribute (Qt::WidgetAttribute attribute, bool enable=true)
 
void setAutoFillBackground (bool enable)
 
void setBackgroundRole (QPalette::ColorRole role)
 
void setBaseSize (const QSize &size)
 
void setBaseSize (int basew, int baseh)
 
void setContentsMargins (const QMargins &margins)
 
void setContentsMargins (int left, int top, int right, int bottom)
 
void setContextMenuPolicy (Qt::ContextMenuPolicy policy)
 
void setCursor (const QCursor &cursor)
 
void setEditFocus (bool enable)
 
void setFixedHeight (int h)
 
void setFixedSize (const QSize &size)
 
void setFixedSize (int w, int h)
 
void setFixedWidth (int w)
 
void setFocus (Qt::FocusReason reason)
 
void setFocusPolicy (Qt::FocusPolicy policy)
 
void setFocusProxy (QWidget *widget)
 
void setFont (const QFont &font)
 
void setForegroundRole (QPalette::ColorRole role)
 
void setGeometry (const QRect &rect)
 
void setGeometry (int x, int y, int w, int h)
 
void setGraphicsEffect (QGraphicsEffect *effect)
 
void setInputMethodHints (Qt::InputMethodHints hints)
 
void setLayout (QLayout *layout)
 
void setLayoutDirection (Qt::LayoutDirection direction)
 
void setLocale (const QLocale &locale)
 
void setMask (const QBitmap &bitmap)
 
void setMask (const QRegion &region)
 
void setMaximumHeight (int maxh)
 
void setMaximumSize (const QSize &size)
 
void setMaximumSize (int maxw, int maxh)
 
void setMaximumWidth (int maxw)
 
void setMinimumHeight (int minh)
 
void setMinimumSize (const QSize &size)
 
void setMinimumSize (int minw, int minh)
 
void setMinimumWidth (int minw)
 
void setMouseTracking (bool enable)
 
void setPalette (const QPalette &palette)
 
void setParent (QWidget *parent)
 
void setParent (QWidget *parent, Qt::WindowFlags flags)
 
void setShortcutAutoRepeat (int id, bool enable=true)
 
void setShortcutEnabled (int id, bool enable=true)
 
void setSizeIncrement (const QSize &size)
 
void setSizeIncrement (int w, int h)
 
void setSizePolicy (QSizePolicy policy)
 
void setSizePolicy (QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical)
 
void setStatusTip (const QString &data)
 
void setStyle (QStyle *style)
 
void setToolTip (const QString &data)
 
void setToolTipDuration (int msec)
 
void setUpdatesEnabled (bool enable)
 
void setWhatsThis (const QString &str)
 
void setWindowFilePath (const QString &filePath)
 
void setWindowFlags (Qt::WindowFlags flags)
 
void setWindowIcon (const QIcon &icon)
 
void setWindowModality (Qt::WindowModality windowModality)
 
void setWindowOpacity (qreal level)
 
void setWindowRole (const QString &role)
 
void setWindowState (Qt::WindowStates windowState)
 
QSize size () const
 
QSize sizeIncrement () const
 
QSizePolicy sizePolicy () const
 
void stackUnder (QWidget *widget)
 
QString statusTip () const
 
QStylestyle () const
 
QString styleSheet () const
 
bool testAttribute (Qt::WidgetAttribute attribute) const
 
QString toolTip () const
 
int toolTipDuration () const
 
QWidget * topLevelWidget () const
 
bool underMouse () const
 
void ungrabGesture (Qt::GestureType gestureType)
 
void unsetCursor ()
 
void unsetLayoutDirection ()
 
void unsetLocale ()
 
void update (const QRect &rect)
 
void update (const QRegion &region)
 
void update (int x, int y, int w, int h)
 
void updateGeometry ()
 
bool updatesEnabled () const
 
QRegion visibleRegion () const
 
QString whatsThis () const
 
int width () const
 
QWidget * window () const
 
QString windowFilePath () const
 
Qt::WindowFlags windowFlags () const
 
QWindowwindowHandle () const
 
QIcon windowIcon () const
 
Qt::WindowModality windowModality () const
 
qreal windowOpacity () const
 
QString windowRole () const
 
Qt::WindowStates windowState () const
 
QString windowTitle () const
 
Qt::WindowType windowType () const
 
WId winId () const
 
int x () const
 
int y () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 
- Public Methods inherited from QPaintDevice
virtual ~QPaintDevice ()
 
int colorCount () const
 
int depth () const
 
int devicePixelRatio () const
 
qreal devicePixelRatioF () const
 
int height () const
 
int heightMM () const
 
int logicalDpiX () const
 
int logicalDpiY () const
 
bool paintingActive () const
 
int physicalDpiX () const
 
int physicalDpiY () const
 
int width () const
 
int widthMM () const
 

Static Public Methods

static QActionexec (const QList< QAction * > &actionList, const QPoint &point, QAction *action=nullptr, QWidget *parent=nullptr)
 
- Static Public Methods inherited from QWidget
static QWidget * createWindowContainer (QWindow *window, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::EmptyFlag)
 
static QWidget * find (WId id)
 
static QWidget * keyboardGrabber ()
 
static QWidget * mouseGrabber ()
 
static void setTabOrder (QWidget *firstWidget, QWidget *secondWidget)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Protected Methods

void actionEvent (QActionEvent *event) override
 
void changeEvent (QEvent *event) override
 
int columnCount () const
 
void enterEvent (QEvent *event) override
 
bool event (QEvent *event) override
 
bool focusNextPrevChild (bool next) override
 
void hideEvent (QHideEvent *event) override
 
void initStyleOption (QStyleOptionMenuItem *option, const QAction *action) const
 
void keyPressEvent (QKeyEvent *event) override
 
void leaveEvent (QEvent *event) override
 
void mouseMoveEvent (QMouseEvent *event) override
 
void mousePressEvent (QMouseEvent *event) override
 
void mouseReleaseEvent (QMouseEvent *event) override
 
void paintEvent (QPaintEvent *event) override
 
void timerEvent (QTimerEvent *event) override
 
void wheelEvent (QWheelEvent *event) override
 
- Protected Methods inherited from QWidget
virtual void closeEvent (QCloseEvent *event)
 
virtual void contextMenuEvent (QContextMenuEvent *event)
 
void create (WId window=0, bool initializeWindow=true, bool destroyOldWindow=true)
 
void destroy (bool destroyWindow=true, bool destroySubWindows=true)
 
virtual void dragEnterEvent (QDragEnterEvent *event)
 
virtual void dragLeaveEvent (QDragLeaveEvent *event)
 
virtual void dragMoveEvent (QDragMoveEvent *event)
 
virtual void dropEvent (QDropEvent *event)
 
bool event (QEvent *event) override
 
virtual void focusInEvent (QFocusEvent *event)
 
bool focusNextChild ()
 
virtual void focusOutEvent (QFocusEvent *event)
 
bool focusPreviousChild ()
 
void initPainter (QPainter *painter) const override
 
virtual void inputMethodEvent (QInputMethodEvent *event)
 
virtual void keyReleaseEvent (QKeyEvent *event)
 
int metric (PaintDeviceMetric metric) const override
 
virtual void mouseDoubleClickEvent (QMouseEvent *event)
 
virtual void moveEvent (QMoveEvent *event)
 
virtual bool nativeEvent (const QByteArray &eventType, void *message, long *result)
 
virtual void resizeEvent (QResizeEvent *event)
 
virtual void showEvent (QShowEvent *event)
 
virtual void tabletEvent (QTabletEvent *event)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Protected Methods inherited from QPaintDevice
 QPaintDevice ()
 

Properties

 icon
 
 separatorsCollapsible
 
 tearOffEnabled
 
 title
 
 toolTipsVisible
 
- Properties inherited from QWidget
 acceptDrops
 
 accessibleDescription
 
 accessibleName
 
 autoFillBackground
 
 baseSize
 
 childrenRect
 
 childrenRegion
 
 contextMenuPolicy
 
 cursor
 
 enabled
 
 focus
 
 focusPolicy
 
 font
 
 frameGeometry
 
 frameSize
 
 fullScreen
 
 geometry
 
 height
 
 inputMethodHints
 
 isActiveWindow
 
 layoutDirection
 
 locale
 
 maximized
 
 maximumHeight
 
 maximumSize
 
 maximumWidth
 
 minimized
 
 minimumHeight
 
 minimumSize
 
 minimumSizeHint
 
 minimumWidth
 
 modal
 
 mouseTracking
 
 normalGeometry
 
 palette
 
 pos
 
 rect
 
 size
 
 sizeHint
 
 sizeIncrement
 
 sizePolicy
 
 statusTip
 
 styleSheet
 
 toolTip
 
 updatesEnabled
 
 visible
 
 whatsThis
 
 width
 
 windowFilePath
 
 windowFlags
 
 windowIcon
 
 windowIconText
 
 windowModality
 
 windowModified
 
 windowOpacity
 
 windowTitle
 
 x
 
 y
 
- Properties inherited from QObject
 objectName
 

Friends

class QAction
 
class QComboBox
 
class QMenuBar
 

Additional Inherited Members

- Public Typedefs inherited from QWidget
using RenderFlags = QFlags< RenderFlag >
 
- Public Types inherited from QWidget
enum  RenderFlag
 
- Public Types inherited from QPaintDevice
enum  PaintDeviceMetric
 
- Public Slots inherited from QWidget
bool close ()
 
void hide ()
 
void lower ()
 
void raise ()
 
void repaint ()
 
void setDisabled (bool disable)
 
void setEnabled (bool enable)
 
void setFocus ()
 
void setHidden (bool hidden)
 
void setStyleSheet (const QString &styleSheet)
 
virtual void setVisible (bool visible)
 
void setWindowModified (bool modified)
 
void setWindowTitle (const QString &title)
 
void show ()
 
void showFullScreen ()
 
void showMaximized ()
 
void showMinimized ()
 
void showNormal ()
 
void update ()
 
- Public Slots inherited from QObject
void deleteLater ()
 
- Protected Slots inherited from QWidget
void updateMicroFocus ()
 

Detailed Description

The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.

A menu widget is a selection menu. It can be either a pull-down menu in a menu bar or a standalone context menu. Pull-down menus are shown by the menu bar when the user clicks on the respective item or presses the specified shortcut key. Use QMenuBar::addMenu() to insert a menu into a menu bar. Context menus are usually invoked by some special keyboard key or by right-clicking. They can be executed either asynchronously with popup() or synchronously with exec(). Menus can also be invoked in response to button presses; these are just like context menus except for how they are invoked.

Windows XP widget style Mac OS X widget style Fusion widget style

Actions

A menu consists of a list of action items. Actions are added with the addAction(), addActions() and insertAction() methods. An action is represented vertically and rendered by QStyle. In addition, actions can have a text label, an optional icon drawn on the very left side, and shortcut key sequence such as "Ctrl+X".

The existing actions held by a menu can be found with actions().

There are four kinds of action items: separators, actions that show a submenu, widgets, and actions that perform an action. Separators are inserted with addSeparator(), submenus with addMenu(), and all other items are considered action items.

When inserting action items you usually specify a receiver and a slot. The receiver will be notified whenever the item is triggered(). In addition, QMenu provides two signals, activated() and highlighted(), which signal the QAction that was triggered from the menu.

You clear a menu with clear() and remove individual action items with removeAction().

A QMenu can also provide a tear-off menu. A tear-off menu is a top-level window that contains a copy of the menu. This makes it possible for the user to "tear off" frequently used menus and position them in a convenient place on the screen. If you want this functionality for a particular menu, insert a tear-off handle with setTearOffEnabled(). When using tear-off menus, bear in mind that the concept is not typically used on Microsoft Windows so some users may not be familiar with it. Consider using a QToolBar instead.

Widgets can be inserted into menus with the QWidgetAction class. Instances of this class are used to hold widgets, and are inserted into menus with the addAction() overload that takes a QAction.

Conversely, actions can be added to widgets with the addAction(), addActions() and insertAction() methods.

Warning
To make QMenu visible on the screen, exec() or popup() should be used instead of show().

QMenu on OS X using Cocoa

A given QMenu can be inserted only once in a menu or menubar. Subsequent insertions will have no effect or will result in a disabled menu item.

See also
QMenuBar

Constructor & Destructor Documentation

QMenu::QMenu ( QWidget parent = nullptr)
explicit

Constructs a menu with the given 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.

QMenu::QMenu ( const QString title,
QWidget parent = nullptr 
)
explicit

Constructs a menu with a title and the given 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.

See also
title
QMenu::~QMenu ( )

Destroys the menu.

Method Documentation

void QMenu::aboutToHide ( )
signal

This signal is emitted just before the menu is hidden from the user.

See also
aboutToShow(), hide()
void QMenu::aboutToShow ( )
signal

This signal is emitted just before the menu is shown to the user.

See also
aboutToHide(), show()
QAction * QMenu::actionAt ( const QPoint point) const

Returns the item at point. Returns a nullptr if there is no item there.

void QMenu::actionEvent ( QActionEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::actionEvent()

QRect QMenu::actionGeometry ( QAction action) const

Returns the geometry of action.

QAction * QMenu::activeAction ( ) const

Returns the currently highlighted action or a nullptr if no action is currently highlighted.

See also
setActiveAction()
QAction * QMenu::addAction ( const QIcon icon,
const QString text 
)

Creates a new action with an icon and some text. The method adds the new action to the menu and returns a pointer to the new QAction.

See also
QWidget::addAction()
QAction * QMenu::addAction ( const QIcon icon,
const QString text,
const QObject receiver,
const QString member,
const QKeySequence shortcut = 0 
)

Creates a new action with an icon and some text and an optional shortcut. The action's triggered() signal is connected to the member slot of the receiver object. The method adds the newly created action to the menu's list of actions and returns a pointer to the new QAction.

See also
QWidget::addAction()
template<typename Func1 >
QAction * QMenu::addAction ( const QIcon icon,
const QString text,
Func1  slot,
const QKeySequence shortcut = QKeySequence() 
)
inline

Creates a new action with the given text and an optional shortcut. The action's triggered() signal is connected to the slot parameter. This method adds the new action to the menu and returns a pointer to the new action.

QMenu takes ownership of the returned QAction.

QAction * QMenu::addAction ( const QString text)

Creates a new action with text. The method adds the new action to the menu and returns a pointer to the new QAction.

See also
QWidget::addAction()
QAction * QMenu::addAction ( const QString text,
const QObject receiver,
const QString member,
const QKeySequence shortcut = QKeySequence() 
)

Creates a new action with the given text and an optional shortcut. The action's triggered() signal is connected to the receivers member slot. The method adds the newly created action to the menu and returns a pointer to the new QAction.

See also
QWidget::addAction()
void QWidget::addAction ( QAction action)
using

Appends action to the list of existing actions available for this widget. A QAction list is typically used to create a context QMenu, however it can be represented in different ways depending on the widget.

A QWidget should only have one of each action. Adding an action which already exists will not cause the same action to be in the widget multiple times. The ownership of action is not transferred to this QWidget.

See also
removeAction(), insertAction(), actions(), QMenu
QMenu * QMenu::addMenu ( const QIcon icon,
const QString title 
)

Appends a new QMenu with icon and title to the menu. The current QMenu takes ownership of the new menu. Returns a pointer to the new menu.

See also
QWidget::addAction(), QMenu::menuAction()
QMenu * QMenu::addMenu ( const QString title)

Appends a new QMenu with title to the menu. The current QMenu takes ownership of the new menu. Returns a pointer to the new menu.

See also
QWidget::addAction(), QMenu::menuAction()
QAction * QMenu::addMenu ( QMenu *  menu)

Adds menu as a submenu to the current menu. Returns a pointer to the menuAction. This menu does not take ownership of menu.

See also
QWidget::addAction(), QMenu::menuAction()
QAction * QMenu::addSection ( const QIcon icon,
const QString text 
)

Creates a new action then calls QAction::setSeparator() and passes true. The hint for this action is set to the given text and icon. This method adds the new action to the menu and returns a pointer to the new action.

The rendering of the text hint is style and platform dependent. Widget styles can use the text information in the rendering for sections or choose to ignore it and render sections like simple separators.

QMenu takes ownership of the returned QAction.

See also
QWidget::addAction()
QAction * QMenu::addSection ( const QString text)

Creates a new action then calls QAction::setSeparator() and passes true. The hint for this action is set to the given text. This method adds the new action to the menu and returns a pointer to the new action.

The rendering of the text hint is style and platform dependent. Widget styles can use the text information in the rendering for sections or choose to ignore it and render sections like simple separators.

QMenu takes ownership of the returned QAction.

See also
addAction()
QAction * QMenu::addSeparator ( )

This method 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.

See also
QWidget::addAction()
void QMenu::changeEvent ( QEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::changeEvent()

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()
int QMenu::columnCount ( ) const
protected

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 method returns the number of columns necessary.

QAction * QMenu::defaultAction ( ) const

Returns the current default action.

See also
setDefaultAction()
void QMenu::enterEvent ( QEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::enterEvent()

bool QMenu::event ( QEvent event)
overrideprotected

Reimplemented from QWidget::event()

QAction * QMenu::exec ( )

Shows the current menu and waits for the user to select an item. Returns the triggered QAction in either the popup menu or one of its submenus, or a nullptr if no item was triggered (normally because the user pressed Esc).

Calling this method is equivalent to exec(pos()).

QAction * QMenu::exec ( const QList< QAction * > &  actionList,
const QPoint point,
QAction action = nullptr,
QWidget parent = nullptr 
)
static

Shows the current menu and waits for the user to select an item. Returns the triggered QAction in either the popup menu or one of its submenus, or a nullptr if no item was triggered (normally because the user pressed Esc).

The menu's actions are specified by the actionList. If you specify an action, then that action will appear at global position point. If no action is not specified then the top left corner of the menu appears at position point. The 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. This can occur with multiple desktops or when the parent is embedded in QGraphicsView.

See also
popup(), QWidget::mapToGlobal()
QAction * QMenu::exec ( const QPoint point,
QAction action = nullptr 
)

Shows the current menu and waits for the user to select an item. Returns the triggered QAction in either the popup menu or one of its submenus, or a nullptr if no item was triggered (normally because the user pressed Esc).

Pops up the menu so the action will be at the specified point. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal(). All signals are emitted as usual. If you connect a QAction to a slot and call QMenu::exec(), the result will be available in the slot and also in the return value of exec().

A common use case is to position the menu at the current mouse position.

This code will align the menu to "somewidget".

exec(somewidget.mapToGlobal(QPoint(0, 0)));

The following code will display the menu at the coordinates where the mouse was clicked.

exec(event->globalPos());

When positioning a menu with exec() or popup(), bear in mind that you can not 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(), QWidget::mapToGlobal()
bool QMenu::focusNextPrevChild ( bool  next)
overrideprotectedvirtual

Reimplemented from QWidget::focusNextPrevChild()

void QMenu::hideEvent ( QHideEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::hideEvent()

void QMenu::hideTearOffMenu ( )

This method will hide the torn off menu making it disappear from the users desktop.

See also
isTearOffMenuVisible(), isTearOffEnabled()
void QMenu::hovered ( QAction action)
signal

This signal is emitted when a menu action is highlighted, action is the action that caused the signal to be emitted. This is used to update status information.

See also
triggered(), QAction::hovered()
QIcon QMenu::icon ( ) const

Returns the value of the property.

void QMenu::initStyleOption ( QStyleOptionMenuItem option,
const QAction action 
) const
protected

Initialize option with the values from this menu and information from action. This method is useful for subclasses when they need a QStyleOptionMenuItem, but do not want to fill in all the information themselves.

See also
QStyleOption::initFrom(), QMenuBar::initStyleOption()
QAction * QMenu::insertMenu ( QAction action,
QMenu *  menu 
)

This method inserts menu before action. Returns the result of calling QMenu::menuAction().

See also
QWidget::insertAction(), addMenu()
QAction * QMenu::insertSection ( QAction location,
const QIcon icon,
const QString text 
)

Creates a new action then calls QAction::setSeparator() and passes true. The hint for this action is set to the given text and icon. This method inserts the new action at the given location in the menu and returns a pointer to the new action.

The rendering of the text hint is style and platform dependent. Widget styles can use the text information in the rendering for sections or choose to ignore it and render sections like simple separators.

QMenu takes ownership of the returned QAction.

See also
QWidget::insertAction(), addSection()
QAction * QMenu::insertSection ( QAction location,
const QString text 
)

Creates a new action then calls QAction::setSeparator() and passes true. The hint for this action is set to the given text. This method inserts the new action at the given location in the menu and returns a pointer to the new action.

The rendering of the text hint is style and platform dependent. Widget styles can use the text information in the rendering for sections or choose to ignore it and render sections like simple separators.

QMenu takes ownership of the returned QAction.

See also
QWidget::insertAction(), addSection()
QAction * QMenu::insertSeparator ( QAction action)

This method creates a new separator action and inserts it before the action. Returns a pointer to the new action.

See also
QWidget::insertAction(), addSeparator()
bool QMenu::isEmpty ( ) const

Returns true if there are no visible actions inserted into the menu, false otherwise.

See also
QWidget::actions()
bool QMenu::isTearOffEnabled ( ) const

Returns the value of the property.

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
hideTearOffMenu(), isTearOffEnabled()
void QMenu::keyPressEvent ( QKeyEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::keyPressEvent()

void QMenu::leaveEvent ( QEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::leaveEvent()

QAction * QMenu::menuAction ( ) const

Returns the action associated with this menu.

void QMenu::mouseMoveEvent ( QMouseEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::mouseMoveEvent()

void QMenu::mousePressEvent ( QMouseEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::mousePressEvent()

void QMenu::mouseReleaseEvent ( QMouseEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::mouseReleaseEvent()

void QMenu::paintEvent ( QPaintEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::paintEvent()

void QMenu::popup ( const QPoint point,
QAction action = nullptr 
)

Displays the menu with the action at the specified point. To translate a widget's local coordinates into global coordinates use QWidget::mapToGlobal(). When specifying a position for a menu using exec() or popup(), do not calculate the position based on the menu's current size(). Instead use sizeHint() which calculates the proper size depending on the menu's current contents.

See also
QWidget::mapToGlobal(), exec()
bool QMenu::separatorsCollapsible ( ) const

Returns the value of the property.

void QMenu::setActiveAction ( QAction action)

Sets the currently highlighted action to action.

See also
activeAction()
void QMenu::setAsDockMenu ( )

Set this menu as the dock menu. Available on Mac OS X only.

void QMenu::setDefaultAction ( QAction action)

This sets the default action to action. 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::setIcon ( const QIcon icon)

Sets the value of the property to icon.

void QMenu::setSeparatorsCollapsible ( bool  collapse)

Sets the value of the property to collapse.

void QMenu::setTearOffEnabled ( bool  enabled)

Sets the value of the property to enabled.

void QMenu::setTitle ( const QString title)

Sets the value of the property to title.

void QMenu::setToolTipsVisible ( bool  visible)

Sets the value of the property to visible.

QSize QMenu::sizeHint ( ) const
overridevirtual

Reimplemented from QWidget::sizeHint()

void QMenu::timerEvent ( QTimerEvent event)
overrideprotected

Reimplemented from QObject::timerEvent()

QString QMenu::title ( ) const

Returns the value of the property.

NSMenu * QMenu::toNSMenu ( )

Returns the native NSMenu for this menu. Available on Mac OS X only.

bool QMenu::toolTipsVisible ( ) const

Returns the value of the property.

void QMenu::triggered ( QAction action)
signal

This signal is emitted when an action in this menu is triggered. The 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(), QAction::triggered()
void QMenu::wheelEvent ( QWheelEvent event)
overrideprotectedvirtual

Reimplemented from QWidget::wheelEvent()

Property Documentation

QMenu::icon

This property holds the icon of the menu. This is equivalent to the QAction::icon property of the menuAction().

By default, if no icon is explicitly set, this property contains a null icon.

PropertiesClass Methods
read icon
write setIcon
QMenu::separatorsCollapsible

This property holds whether consecutive separators should be collapsed. This property specifies whether consecutive separators in the menu should be visually collapsed to a single one. Separators at the beginning or the end of the menu are also hidden. By default this property is true.

PropertiesClass Methods
read separatorsCollapsible
write setSeparatorsCollapsible
QMenu::tearOffEnabled

This property holds whether the menu supports being torn off. When true, the menu contains a special tear-off item (often shown as a dashed line at the top of the menu) that creates a copy of the menu when it is triggered.

This "torn-off" copy lives in a separate window. It contains the same menu items as the original menu, with the exception of the tear-off handle.

By default, this property is false.

PropertiesClass Methods
read isTearOffEnabled
write setTearOffEnabled
QMenu::title

This property holds the title of the menu. This is equivalent to the QAction::text property of the menuAction().

By default, this property contains an empty string.

PropertiesClass Methods
read title
write setTitle
QMenu::toolTipsVisible

This property specifies whether action menu entries show their tooltip.

By default this property is false.

PropertiesClass Methods
read toolTipsVisible
write setToolTipsVisible