Qt 4.8
Signals | Public Functions | Static Public Functions | Protected Functions | Private Slots | Properties | Friends | List of all members
QMenu Class Reference

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

#include <qmenu.h>

Inheritance diagram for QMenu:
QWidget QObject QPaintDevice QSystemTrayIconQMenu QTornOffMenu QUnicodeControlCharacterMenu

Signals

void aboutToHide ()
 This signal is emitted just before the menu is hidden from the user. More...
 
void aboutToShow ()
 This signal is emitted just before the menu is shown to the user. More...
 
void hovered (QAction *action)
 This signal is emitted when a menu action is highlighted; action is the action that caused the signal to be emitted. More...
 
void triggered (QAction *action)
 This signal is emitted when an action in this menu is triggered. More...
 
- Signals inherited from QWidget
void customContextMenuRequested (const QPoint &pos)
 This signal is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu, and the user has requested a context menu on the widget. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 

Public Functions

QActionactionAt (const QPoint &) const
 Returns the item at pt; returns 0 if there is no item there. More...
 
QRect actionGeometry (QAction *) const
 Returns the geometry of action act. More...
 
QActionactiveAction () const
 Returns the currently highlighted action, or 0 if no action is currently highlighted. More...
 
QActionaddAction (const QString &text)
 This convenience function creates a new action with text. More...
 
QActionaddAction (const QIcon &icon, const QString &text)
 This convenience function creates a new action with an icon and some text. More...
 
QActionaddAction (const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut=0)
 This convenience function creates a new action with the text text and an optional shortcut shortcut. More...
 
QActionaddAction (const QIcon &icon, const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut=0)
 This convenience function creates a new action with an icon and some text and an optional shortcut shortcut. More...
 
QActionaddMenu (QMenu *menu)
 This convenience function adds menu as a submenu to this menu. More...
 
QMenuaddMenu (const QString &title)
 Appends a new QMenu with title to the menu. More...
 
QMenuaddMenu (const QIcon &icon, const QString &title)
 Appends a new QMenu with icon and title to the menu. More...
 
QActionaddSeparator ()
 This convenience function creates a new separator action, i.e. More...
 
void clear ()
 Removes all the menu's actions. More...
 
QActiondefaultAction () const
 Returns the current default action. More...
 
QActionexec ()
 Executes this menu synchronously. More...
 
QActionexec (const QPoint &pos, QAction *at=0)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Executes this menu synchronously. More...
 
void hideTearOffMenu ()
 This function will forcibly hide the torn off menu making it disappear from the users desktop. More...
 
QIcon icon () const
 
QActioninsertMenu (QAction *before, QMenu *menu)
 This convenience function inserts menu before action before and returns the menus menuAction(). More...
 
QActioninsertSeparator (QAction *before)
 This convenience function creates a new separator action, i.e. More...
 
bool isEmpty () const
 Returns true if there are no visible actions inserted into the menu, false otherwise. More...
 
bool isTearOffEnabled () const
 
bool isTearOffMenuVisible () const
 When a menu is torn off a second menu is shown to display the menu contents in a new window. More...
 
OSMenuRef macMenu (OSMenuRef merge=0)
 
QActionmenuAction () const
 Returns the action associated with this menu. More...
 
void popup (const QPoint &pos, QAction *at=0)
 Displays the menu so that the action atAction will be at the specified global position p. More...
 
 QMenu (QWidget *parent=0)
 Constructs a menu with parent parent. More...
 
 QMenu (const QString &title, QWidget *parent=0)
 Constructs a menu with a title and a parent. More...
 
bool separatorsCollapsible () const
 
void setActiveAction (QAction *act)
 Sets the currently highlighted action to act. More...
 
void setDefaultAction (QAction *)
 This sets the default action to act. More...
 
void setIcon (const QIcon &icon)
 
void setNoReplayFor (QWidget *widget)
 
void setSeparatorsCollapsible (bool collapse)
 
void setTearOffEnabled (bool)
 
void setTitle (const QString &title)
 
QSize sizeHint () const
 Reimplemented Function More...
 
QString title () const
 
HMENU wceMenu ()
 This function will return the HMENU used to create the native Windows CE menu bar bindings. More...
 
 ~QMenu ()
 Destroys the menu. More...
 
- Public Functions inherited from QWidget
bool acceptDrops () const
 
QString accessibleDescription () const
 
QString accessibleName () const
 
QList< QAction * > actions () const
 Returns the (possibly empty) list of this widget's actions. More...
 
void activateWindow ()
 Sets the top-level widget containing this widget to be the active window. More...
 
void addAction (QAction *action)
 Appends the action action to this widget's list of actions. More...
 
void addActions (QList< QAction *> actions)
 Appends the actions actions to this widget's list of actions. More...
 
void adjustSize ()
 Adjusts the size of the widget to fit its contents. More...
 
bool autoFillBackground () const
 
QPalette::ColorRole backgroundRole () const
 Returns the background role of the widget. More...
 
QSize baseSize () const
 
QWidgetchildAt (int x, int y) const
 Returns the visible child widget at the position ({x}, {y}) in the widget's coordinate system. More...
 
QWidgetchildAt (const QPoint &p) const
 Returns the visible child widget at point p in the widget's own coordinate system. More...
 
QRect childrenRect () const
 
QRegion childrenRegion () const
 
void clearFocus ()
 Takes keyboard input focus from the widget. More...
 
void clearMask ()
 Removes any mask set by setMask(). More...
 
QMargins contentsMargins () const
 The contentsMargins function returns the widget's contents margins. More...
 
QRect contentsRect () const
 Returns the area inside the widget's margins. More...
 
Qt::ContextMenuPolicy contextMenuPolicy () const
 
void createWinId ()
 
QCursor cursor () const
 
int devType () const
 
WId effectiveWinId () const
 Returns the effective window system identifier of the widget, i. More...
 
void ensurePolished () const
 Ensures that the widget has been polished by QStyle (i.e., has a proper font and palette). More...
 
Qt::FocusPolicy focusPolicy () const
 
QWidgetfocusProxy () const
 Returns the focus proxy, or 0 if there is no focus proxy. More...
 
QWidgetfocusWidget () const
 Returns the last child of this widget that setFocus had been called on. More...
 
const QFontfont () const
 
QFontInfo fontInfo () const
 Returns the font info for the widget's current font. More...
 
QFontMetrics fontMetrics () const
 Returns the font metrics for the widget's current font. More...
 
QPalette::ColorRole foregroundRole () const
 Returns the foreground role. More...
 
QRect frameGeometry () const
 
QSize frameSize () const
 
const QRectgeometry () const
 
void getContentsMargins (int *left, int *top, int *right, int *bottom) const
 Returns the widget's contents margins for left, top, right, and bottom. More...
 
HDC getDC () const
 Returns the window system handle of the widget, for low-level access. More...
 
void grabGesture (Qt::GestureType type, Qt::GestureFlags flags=Qt::GestureFlags())
 Subscribes the widget to a given gesture with specific flags. More...
 
void grabKeyboard ()
 Grabs the keyboard input. More...
 
void grabMouse ()
 Grabs the mouse input. More...
 
void grabMouse (const QCursor &)
 
int grabShortcut (const QKeySequence &key, Qt::ShortcutContext context=Qt::WindowShortcut)
 Adds a shortcut to Qt's shortcut system that watches for the given key sequence in the given context. More...
 
QGraphicsEffectgraphicsEffect () const
 The graphicsEffect function returns a pointer to the widget's graphics effect. More...
 
QGraphicsProxyWidgetgraphicsProxyWidget () const
 Returns the proxy widget for the corresponding embedded widget in a graphics view; otherwise returns 0. More...
 
bool hasFocus () const
 
bool hasMouseTracking () const
 
int height () const
 
virtual int heightForWidth (int) const
 Returns the preferred height for this widget, given the width w. More...
 
QInputContextinputContext ()
 This function returns the QInputContext for this widget. More...
 
Qt::InputMethodHints inputMethodHints () const
 
virtual QVariant inputMethodQuery (Qt::InputMethodQuery) const
 This method is only relevant for input widgets. More...
 
void insertAction (QAction *before, QAction *action)
 Inserts the action action to this widget's list of actions, before the action before. More...
 
void insertActions (QAction *before, QList< QAction *> actions)
 Inserts the actions actions to this widget's list of actions, before the action before. More...
 
WId internalWinId () const
 Returns the window system identifier of the widget, or 0 if the widget is not created yet. More...
 
bool isActiveWindow () const
 
bool isAncestorOf (const QWidget *child) const
 Returns true if this widget is a parent, (or grandparent and so on to any level), of the given child, and both widgets are within the same window; otherwise returns false. More...
 
bool isEnabled () const
 
bool isEnabledTo (QWidget *) const
 Returns true if this widget would become enabled if ancestor is enabled; otherwise returns false. More...
 
bool isEnabledToTLW () const
 This function is deprecated. More...
 
bool isFullScreen () const
 
bool isHidden () const
 Returns true if the widget is hidden, otherwise returns false. More...
 
bool isLeftToRight () const
 
bool isMaximized () const
 
bool isMinimized () const
 
bool isModal () const
 
bool isRightToLeft () const
 
bool isTopLevel () const
 Use isWindow() instead. More...
 
bool isVisible () const
 
bool isVisibleTo (QWidget *) const
 Returns true if this widget would become visible if ancestor is shown; otherwise returns false. More...
 
bool isWindow () const
 Returns true if the widget is an independent window, otherwise returns false. More...
 
bool isWindowModified () const
 
QLayoutlayout () const
 Returns the layout manager that is installed on this widget, or 0 if no layout manager is installed. More...
 
Qt::LayoutDirection layoutDirection () const
 
QLocale locale () const
 
Qt::HANDLE macCGHandle () const
 Returns the CoreGraphics handle of the widget. More...
 
Qt::HANDLE macQDHandle () const
 Returns the QuickDraw handle of the widget. More...
 
QPoint mapFrom (QWidget *, const QPoint &) const
 Translates the widget coordinate pos from the coordinate system of parent to this widget's coordinate system. More...
 
QPoint mapFromGlobal (const QPoint &) const
 Translates the global screen coordinate pos to widget coordinates. More...
 
QPoint mapFromParent (const QPoint &) const
 Translates the parent widget coordinate pos to widget coordinates. More...
 
QPoint mapTo (QWidget *, const QPoint &) const
 Translates the widget coordinate pos to the coordinate system of parent. More...
 
QPoint mapToGlobal (const QPoint &) const
 Translates the widget coordinate pos to global screen coordinates. More...
 
QPoint mapToParent (const QPoint &) const
 Translates the widget coordinate pos to a coordinate in the parent widget. More...
 
QRegion mask () const
 Returns the mask currently set on a widget. More...
 
int maximumHeight () const
 
QSize maximumSize () const
 
int maximumWidth () const
 
int minimumHeight () const
 
QSize minimumSize () const
 
virtual QSize minimumSizeHint () const
 
int minimumWidth () const
 
void move (int x, int y)
 This corresponds to move(QPoint(x, y)). More...
 
void move (const QPoint &)
 
QWidgetnativeParentWidget () const
 Returns the native parent for this widget, i. More...
 
QWidgetnextInFocusChain () const
 Returns the next widget in this widget's focus chain. More...
 
QRect normalGeometry () const
 
void overrideWindowFlags (Qt::WindowFlags type)
 Sets the window flags for the widget to flags, without telling the window system. More...
 
void overrideWindowState (Qt::WindowStates state)
 
QPaintEnginepaintEngine () const
 Returns the widget's paint engine. More...
 
const QPalettepalette () const
 
QWidgetparentWidget () const
 Returns the parent of this widget, or 0 if it does not have any parent widget. More...
 
QPoint pos () const
 
QWidgetpreviousInFocusChain () const
 The previousInFocusChain function returns the previous widget in this widget's focus chain. More...
 
 QWidget (QWidget *parent=0, Qt::WindowFlags f=0)
 Constructs a widget which is a child of parent, with widget flags set to f. More...
 
QRect rect () const
 
void releaseDC (HDC) const
 Releases the HDC hdc acquired by a previous call to getDC(). More...
 
void releaseKeyboard ()
 Releases the keyboard grab. More...
 
void releaseMouse ()
 Releases the mouse grab. More...
 
void releaseShortcut (int id)
 Removes the shortcut with the given id from Qt's shortcut system. More...
 
void removeAction (QAction *action)
 Removes the action action from this widget's list of actions. More...
 
void render (QPaintDevice *target, const QPoint &targetOffset=QPoint(), const QRegion &sourceRegion=QRegion(), RenderFlags renderFlags=RenderFlags(DrawWindowBackground|DrawChildren))
 Renders the sourceRegion of this widget into the target using renderFlags to determine how to render. More...
 
void render (QPainter *painter, const QPoint &targetOffset=QPoint(), const QRegion &sourceRegion=QRegion(), RenderFlags renderFlags=RenderFlags(DrawWindowBackground|DrawChildren))
 Renders the widget into the painter's QPainter::device(). More...
 
void repaint (int x, int y, int w, int h)
 This version repaints a rectangle (x, y, w, h) inside the widget. More...
 
void repaint (const QRect &)
 This version repaints a rectangle rect inside the widget. More...
 
void repaint (const QRegion &)
 This version repaints a region rgn inside the widget. More...
 
void resize (int w, int h)
 This corresponds to resize(QSize(w, h)). More...
 
void resize (const QSize &)
 
bool restoreGeometry (const QByteArray &geometry)
 Restores the geometry and state top-level widgets stored in the byte array geometry. More...
 
QByteArray saveGeometry () const
 Saves the current geometry and state for top-level widgets. More...
 
void scroll (int dx, int dy)
 Scrolls the widget including its children dx pixels to the right and dy downward. More...
 
void scroll (int dx, int dy, const QRect &)
 This version only scrolls r and does not move the children of the widget. More...
 
void setAcceptDrops (bool on)
 
void setAccessibleDescription (const QString &description)
 
void setAccessibleName (const QString &name)
 
void setAttribute (Qt::WidgetAttribute, bool on=true)
 Sets the attribute attribute on this widget if on is true; otherwise clears the attribute. More...
 
void setAutoFillBackground (bool enabled)
 
void setBackgroundRole (QPalette::ColorRole)
 Sets the background role of the widget to role. More...
 
void setBaseSize (const QSize &)
 
void setBaseSize (int basew, int baseh)
 This corresponds to setBaseSize(QSize(basew, baseh)). More...
 
void setContentsMargins (int left, int top, int right, int bottom)
 Sets the margins around the contents of the widget to have the sizes left, top, right, and bottom. More...
 
void setContentsMargins (const QMargins &margins)
 The setContentsMargins function sets the margins around the widget's contents. More...
 
void setContextMenuPolicy (Qt::ContextMenuPolicy policy)
 
void setCursor (const QCursor &)
 
void setFixedHeight (int h)
 Sets both the minimum and maximum heights of the widget to h without changing the widths. More...
 
void setFixedSize (const QSize &)
 Sets both the minimum and maximum sizes of the widget to s, thereby preventing it from ever growing or shrinking. More...
 
void setFixedSize (int w, int h)
 Sets the width of the widget to w and the height to h. More...
 
void setFixedWidth (int w)
 Sets both the minimum and maximum width of the widget to w without changing the heights. More...
 
void setFocus (Qt::FocusReason reason)
 Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its parents is the active window. More...
 
void setFocusPolicy (Qt::FocusPolicy policy)
 
void setFocusProxy (QWidget *)
 Sets the widget's focus proxy to widget w. More...
 
void setFont (const QFont &)
 Use the single-argument overload instead. More...
 
void setForegroundRole (QPalette::ColorRole)
 Sets the foreground role of the widget to role. More...
 
void setGeometry (int x, int y, int w, int h)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This corresponds to setGeometry(QRect(x, y, w, h)). More...
 
void setGeometry (const QRect &)
 
void setGraphicsEffect (QGraphicsEffect *effect)
 The setGraphicsEffect function is for setting the widget's graphics effect. More...
 
void setInputContext (QInputContext *)
 This function sets the input context context on this widget. More...
 
void setInputMethodHints (Qt::InputMethodHints hints)
 
void setLayout (QLayout *)
 Sets the layout manager for this widget to layout. More...
 
void setLayoutDirection (Qt::LayoutDirection direction)
 
void setLocale (const QLocale &locale)
 
void setMask (const QBitmap &)
 Causes only the pixels of the widget for which bitmap has a corresponding 1 bit to be visible. More...
 
void setMask (const QRegion &)
 Causes only the parts of the widget which overlap region to be visible. More...
 
void setMaximumHeight (int maxh)
 
void setMaximumSize (const QSize &)
 
void setMaximumSize (int maxw, int maxh)
 This function corresponds to setMaximumSize(QSize(maxw, maxh)). More...
 
void setMaximumWidth (int maxw)
 
void setMinimumHeight (int minh)
 
void setMinimumSize (const QSize &)
 
void setMinimumSize (int minw, int minh)
 This function corresponds to setMinimumSize(QSize(minw, minh)). More...
 
void setMinimumWidth (int minw)
 
void setMouseTracking (bool enable)
 
void setPalette (const QPalette &)
 Use the single-argument overload instead. More...
 
void setParent (QWidget *parent)
 Sets the parent of the widget to parent, and resets the window flags. More...
 
void setParent (QWidget *parent, Qt::WindowFlags f)
 This function also takes widget flags, f as an argument. More...
 
void setShortcutAutoRepeat (int id, bool enable=true)
 If enable is true, auto repeat of the shortcut with the given id is enabled; otherwise it is disabled. More...
 
void setShortcutEnabled (int id, bool enable=true)
 If enable is true, the shortcut with the given id is enabled; otherwise the shortcut is disabled. More...
 
void setSizeIncrement (const QSize &)
 
void setSizeIncrement (int w, int h)
 Sets the x (width) size increment to w and the y (height) size increment to h. More...
 
void setSizePolicy (QSizePolicy)
 
void setSizePolicy (QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical)
 Sets the size policy of the widget to horizontal and vertical, with standard stretch and no height-for-width. More...
 
void setStatusTip (const QString &)
 
void setStyle (QStyle *)
 Sets the widget's GUI style to style. More...
 
void setToolTip (const QString &)
 
void setUpdatesEnabled (bool enable)
 
void setupUi (QWidget *widget)
 Sets up the user interface for the specified widget. More...
 
void setWhatsThis (const QString &)
 
void setWindowFilePath (const QString &filePath)
 
void setWindowFlags (Qt::WindowFlags type)
 
void setWindowIcon (const QIcon &icon)
 
void setWindowIconText (const QString &)
 
void setWindowModality (Qt::WindowModality windowModality)
 
void setWindowOpacity (qreal level)
 
void setWindowRole (const QString &)
 Sets the window's role to role. More...
 
void setWindowState (Qt::WindowStates state)
 Sets the window state to windowState. More...
 
void setWindowSurface (QWindowSurface *surface)
 Sets the window surface to be the surface specified. More...
 
QSize size () const
 
QSize sizeIncrement () const
 
QSizePolicy sizePolicy () const
 
void stackUnder (QWidget *)
 Places the widget under w in the parent widget's stack. More...
 
QString statusTip () const
 
QStylestyle () const
 
QString styleSheet () const
 
bool testAttribute (Qt::WidgetAttribute) const
 Returns true if attribute attribute is set on this widget; otherwise returns false. More...
 
QString toolTip () const
 
QWidgettopLevelWidget () const
 
bool underMouse () const
 Returns true if the widget is under the mouse cursor; otherwise returns false. More...
 
void ungrabGesture (Qt::GestureType type)
 Unsubscribes the widget from a given gesture type. More...
 
void unsetCursor ()
 
void unsetLayoutDirection ()
 
void unsetLocale ()
 
void update (int x, int y, int w, int h)
 This version updates a rectangle (x, y, w, h) inside the widget. More...
 
void update (const QRect &)
 This version updates a rectangle rect inside the widget. More...
 
void update (const QRegion &)
 This version repaints a region rgn inside the widget. More...
 
void updateGeometry ()
 Notifies the layout system that this widget has changed and may need to change geometry. More...
 
bool updatesEnabled () const
 
QRegion visibleRegion () const
 Returns the unobscured region where paint events can occur. More...
 
QString whatsThis () const
 
int width () const
 
QWidgetwindow () const
 Returns the window for this widget, i.e. More...
 
QString windowFilePath () const
 
Qt::WindowFlags windowFlags () const
 Window flags are a combination of a type (e. More...
 
QIcon windowIcon () const
 
QString windowIconText () const
 
Qt::WindowModality windowModality () const
 
qreal windowOpacity () const
 
QString windowRole () const
 Returns the window's role, or an empty string. More...
 
Qt::WindowStates windowState () const
 Returns the current window state. More...
 
QWindowSurfacewindowSurface () const
 Returns the QWindowSurface this widget will be drawn into. More...
 
QString windowTitle () const
 
Qt::WindowType windowType () const
 Returns the window type of this widget. More...
 
WId winId () const
 Returns the window system identifier of the widget. More...
 
int x () const
 
const QX11Infox11Info () const
 Returns information about the configuration of the X display used to display the widget. More...
 
Qt::HANDLE x11PictureHandle () const
 Returns the X11 Picture handle of the widget for XRender support. More...
 
int y () const
 
 ~QWidget ()
 Destroys the widget. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 
- Public Functions inherited from QPaintDevice
int colorCount () const
 
int depth () const
 
int height () const
 
int heightMM () const
 
int logicalDpiX () const
 
int logicalDpiY () const
 
QT_DEPRECATED int numColors () const
 
bool paintingActive () const
 
int physicalDpiX () const
 
int physicalDpiY () const
 
int width () const
 
int widthMM () const
 
virtual ~QPaintDevice ()
 

Static Public Functions

static QActionexec (QList< QAction *> actions, const QPoint &pos, QAction *at=0)
 Executes a menu synchronously. More...
 
static QActionexec (QList< QAction *> actions, const QPoint &pos, QAction *at, QWidget *parent)
 Executes a menu synchronously. More...
 
- Static Public Functions inherited from QWidget
static QWidgetfind (WId)
 Returns a pointer to the widget with window identifer/handle id. More...
 
static QWidgetkeyboardGrabber ()
 Returns the widget that is currently grabbing the keyboard input. More...
 
static QWidgetmouseGrabber ()
 Returns the widget that is currently grabbing the mouse input. More...
 
static void setTabOrder (QWidget *, QWidget *)
 Puts the second widget after the first widget in the focus order. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Functions inherited from QPaintDevice
static QWSDisplayqwsDisplay ()
 

Protected Functions

void actionEvent (QActionEvent *)
 Reimplemented Function More...
 
void changeEvent (QEvent *)
 Reimplemented Function More...
 
int columnCount () const
 If a menu does not fit on the screen it lays itself out so that it does fit. More...
 
void enterEvent (QEvent *)
 Reimplemented Function More...
 
bool event (QEvent *)
 Reimplemented Function More...
 
bool focusNextPrevChild (bool next)
 Reimplemented Function More...
 
void hideEvent (QHideEvent *)
 Reimplemented Function More...
 
void initStyleOption (QStyleOptionMenuItem *option, const QAction *action) const
 Initialize option with the values from this menu and information from action. More...
 
void keyPressEvent (QKeyEvent *)
 Reimplemented Function More...
 
void leaveEvent (QEvent *)
 Reimplemented Function More...
 
void mouseMoveEvent (QMouseEvent *)
 Reimplemented Function More...
 
void mousePressEvent (QMouseEvent *)
 Reimplemented Function More...
 
void mouseReleaseEvent (QMouseEvent *)
 Reimplemented Function More...
 
void paintEvent (QPaintEvent *)
 Reimplemented Function More...
 
 QMenu (QMenuPrivate &dd, QWidget *parent=0)
 
void timerEvent (QTimerEvent *)
 Reimplemented Function More...
 
QActionwceCommands (uint command)
 This function sends native Windows CE commands to Qt menus. More...
 
void wheelEvent (QWheelEvent *)
 Reimplemented Function More...
 
- Protected Functions inherited from QWidget
virtual void closeEvent (QCloseEvent *)
 This event handler is called with the given event when Qt receives a window close request for a top-level widget from the window system. More...
 
virtual void contextMenuEvent (QContextMenuEvent *)
 This event handler, for event event, can be reimplemented in a subclass to receive widget context menu events. More...
 
void create (WId=0, bool initializeWindow=true, bool destroyOldWindow=true)
 Creates a new widget window if window is 0, otherwise sets the widget's window to window. More...
 
void destroy (bool destroyWindow=true, bool destroySubWindows=true)
 Frees up window system resources. More...
 
virtual void dragEnterEvent (QDragEnterEvent *)
 This event handler is called when a drag is in progress and the mouse enters this widget. More...
 
virtual void dragLeaveEvent (QDragLeaveEvent *)
 This event handler is called when a drag is in progress and the mouse leaves this widget. More...
 
virtual void dragMoveEvent (QDragMoveEvent *)
 This event handler is called if a drag is in progress, and when any of the following conditions occur: the cursor enters this widget, the cursor moves within this widget, or a modifier key is pressed on the keyboard while this widget has the focus. More...
 
virtual void dropEvent (QDropEvent *)
 This event handler is called when the drag is dropped on this widget. More...
 
virtual void enabledChange (bool)
 
virtual void focusInEvent (QFocusEvent *)
 This event handler can be reimplemented in a subclass to receive keyboard focus events (focus received) for the widget. More...
 
bool focusNextChild ()
 Finds a new widget to give the keyboard focus to, as appropriate for Tab, and returns true if it can find a new widget, or false if it can't. More...
 
virtual void focusOutEvent (QFocusEvent *)
 This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) for the widget. More...
 
bool focusPreviousChild ()
 Finds a new widget to give the keyboard focus to, as appropriate for Shift+Tab, and returns true if it can find a new widget, or false if it can't. More...
 
virtual void fontChange (const QFont &)
 
virtual void inputMethodEvent (QInputMethodEvent *)
 This event handler, for event event, can be reimplemented in a subclass to receive Input Method composition events. More...
 
virtual void keyReleaseEvent (QKeyEvent *)
 This event handler, for event event, can be reimplemented in a subclass to receive key release events for the widget. More...
 
virtual void languageChange ()
 
virtual bool macEvent (EventHandlerCallRef, EventRef)
 This special event handler can be reimplemented in a subclass to receive native Macintosh events. More...
 
int metric (PaintDeviceMetric) const
 Internal implementation of the virtual QPaintDevice::metric() function. More...
 
virtual void mouseDoubleClickEvent (QMouseEvent *)
 This event handler, for event event, can be reimplemented in a subclass to receive mouse double click events for the widget. More...
 
virtual void moveEvent (QMoveEvent *)
 This event handler can be reimplemented in a subclass to receive widget move events which are passed in the event parameter. More...
 
virtual void paletteChange (const QPalette &)
 
 QWidget (QWidgetPrivate &d, QWidget *parent, Qt::WindowFlags f)
 
virtual bool qwsEvent (QWSEvent *)
 This special event handler can be reimplemented in a subclass to receive native Qt for Embedded Linux events which are passed in the event parameter. More...
 
void resetInputContext ()
 This function can be called on the widget that currently has focus to reset the input method operating on it. More...
 
virtual void resizeEvent (QResizeEvent *)
 This event handler can be reimplemented in a subclass to receive widget resize events which are passed in the event parameter. More...
 
virtual void showEvent (QShowEvent *)
 This event handler can be reimplemented in a subclass to receive widget show events which are passed in the event parameter. More...
 
virtual void styleChange (QStyle &)
 
virtual void tabletEvent (QTabletEvent *)
 This event handler, for event event, can be reimplemented in a subclass to receive tablet events for the widget. More...
 
virtual void windowActivationChange (bool)
 
virtual bool winEvent (MSG *message, long *result)
 This special event handler can be reimplemented in a subclass to receive native Windows events which are passed in the message parameter. More...
 
virtual bool x11Event (XEvent *)
 This special event handler can be reimplemented in a subclass to receive native X11 events passed in the event parameter. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
- Protected Functions inherited from QPaintDevice
 QPaintDevice ()
 

Private Slots

void internalDelayedPopup ()
 
void internalSetSloppyAction ()
 

Properties

QIcon icon
 The icon of the menu. More...
 
bool separatorsCollapsible
 whether consecutive separators should be collapsed More...
 
bool tearOffEnabled
 whether the menu supports being torn off More...
 
QString title
 The title of the menu. More...
 

Friends

class Q3PopupMenu
 
class QAction
 
class QComboBox
 
class QMenuBar
 
class QMenuBarPrivate
 
bool qt_mac_activate_action (OSMenuRef, uint, QAction::ActionEvent, bool)
 
void qt_mac_emit_menuSignals (QMenu *, bool)
 
void qt_mac_menu_emit_hovered (QMenu *menu, QAction *action)
 
OSStatus qt_mac_menu_event (EventHandlerCallRef, EventRef, void *)
 
void qt_mac_trayicon_activate_action (QMenu *, QAction *action)
 
bool qt_mac_watchingAboutToShow (QMenu *)
 
class QToolButtonPrivate
 
class QTornOffMenu
 

Additional Inherited Members

- Public Types inherited from QWidget
enum  RenderFlag { DrawWindowBackground = 0x1, DrawChildren = 0x2, IgnoreMask = 0x4 }
 This enum describes how to render the widget when calling QWidget::render(). More...
 
- Public Types inherited from QPaintDevice
enum  PaintDeviceMetric {
  PdmWidth = 1, PdmHeight, PdmWidthMM, PdmHeightMM,
  PdmNumColors, PdmDepth, PdmDpiX, PdmDpiY,
  PdmPhysicalDpiX, PdmPhysicalDpiY
}
 
- Public Slots inherited from QWidget
bool close ()
 Closes this widget. More...
 
void hide ()
 Hides the widget. More...
 
void lower ()
 Lowers the widget to the bottom of the parent widget's stack. More...
 
void raise ()
 Raises this widget to the top of the parent widget's stack. More...
 
void repaint ()
 Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the widget is hidden. More...
 
void setDisabled (bool)
 Disables widget input events if disable is true; otherwise enables input events. More...
 
void setEnabled (bool)
 
void setFocus ()
 Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its parents is the active window. More...
 
void setHidden (bool hidden)
 Convenience function, equivalent to setVisible(!hidden). More...
 
QT_MOC_COMPAT void setShown (bool shown)
 Use setVisible(shown) instead. More...
 
void setStyleSheet (const QString &styleSheet)
 
virtual void setVisible (bool visible)
 
void setWindowModified (bool)
 
void setWindowTitle (const QString &)
 
void show ()
 Shows the widget and its child widgets. More...
 
void showFullScreen ()
 Shows the widget in full-screen mode. More...
 
void showMaximized ()
 Shows the widget maximized. More...
 
void showMinimized ()
 Shows the widget minimized, as an icon. More...
 
void showNormal ()
 Restores the widget after it has been maximized or minimized. More...
 
void update ()
 Updates the widget unless updates are disabled or the widget is hidden. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Slots inherited from QWidget
void updateMicroFocus ()
 Updates the widget's micro focus. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Protected Variables inherited from QPaintDevice
ushort painters
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

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.

plastique-menu.png
windowsxp-menu.png
macintosh-menu.png

Fig. A menu shown in Plastique Style Widget Gallery{Plastique widget style}, Windows XP Style Widget Gallery{Windows XP widget style}, and Macintosh Style Widget Gallery{Macintosh widget style}.

Actions

A menu consists of a list of action items. Actions are added with the addAction(), addActions() and insertAction() functions. 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 notifed 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 isn't 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() functions.

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

QMenu on Qt for Windows CE

If a menu is integrated into the native menubar on Windows Mobile we do not support the signals: aboutToHide (), aboutToShow () and hovered (). It is not possible to display an icon in a native menu on Windows Mobile.

QMenu on Mac OS X with Qt build against Cocoa

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

See the Menus example for an example of how to use QMenuBar and QMenu in your application.

Important inherited functions: addAction(), removeAction(), clear(), addSeparator(), and addMenu().

See also
QMenuBar, {fowler}{GUI Design Handbook: Menu, Drop-Down and Pop-Up}, {Application Example}, {Menus Example}, {Recent Files Example}

Definition at line 72 of file qmenu.h.

Constructors and Destructors

◆ QMenu() [1/3]

QMenu::QMenu ( QWidget parent = 0)
explicit

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).

Definition at line 1386 of file qmenu.cpp.

Referenced by addMenu().

1387  : QWidget(*new QMenuPrivate, parent, Qt::Popup)
1388 {
1389  Q_D(QMenu);
1390  d->init();
1391 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
QWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a widget which is a child of parent, with widget flags set to f.
Definition: qwidget.cpp:1189
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ QMenu() [2/3]

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

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

Definition at line 1402 of file qmenu.cpp.

1403  : QWidget(*new QMenuPrivate, parent, Qt::Popup)
1404 {
1405  Q_D(QMenu);
1406  d->init();
1407  d->menuAction->setText(title);
1408 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
QWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a widget which is a child of parent, with widget flags set to f.
Definition: qwidget.cpp:1189
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ ~QMenu()

QMenu::~QMenu ( )

Destroys the menu.

Definition at line 1422 of file qmenu.cpp.

1423 {
1424  Q_D(QMenu);
1425  if (!d->widgetItems.isEmpty()) { // avoid detach on shared null hash
1426  QHash<QAction *, QWidget *>::iterator it = d->widgetItems.begin();
1427  for (; it != d->widgetItems.end(); ++it) {
1428  if (QWidget *widget = it.value()) {
1429  QWidgetAction *action = static_cast<QWidgetAction *>(it.key());
1430  action->releaseWidget(widget);
1431  *it = 0;
1432  }
1433  }
1434  }
1435 
1436  if (d->eventLoop)
1437  d->eventLoop->exit();
1438  hideTearOffMenu();
1439 }
double d
Definition: qnumeric_p.h:62
QPointer< QWidget > widget
#define it(className, varName)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qhash.h:348
#define Q_D(Class)
Definition: qglobal.h:2482
void hideTearOffMenu()
This function will forcibly hide the torn off menu making it disappear from the users desktop...
Definition: qmenu.cpp:1697
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
The QWidgetAction class extends QAction by an interface for inserting custom widgets into action base...
Definition: qwidgetaction.h:57
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
void releaseWidget(QWidget *widget)
Releases the specified widget.

◆ QMenu() [3/3]

QMenu::QMenu ( QMenuPrivate dd,
QWidget parent = 0 
)
protected
Warning
This function is not part of the public interface.

Definition at line 1412 of file qmenu.cpp.

1413  : QWidget(dd, parent, Qt::Popup)
1414 {
1415  Q_D(QMenu);
1416  d->init();
1417 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
QWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a widget which is a child of parent, with widget flags set to f.
Definition: qwidget.cpp:1189
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

Functions

◆ aboutToHide

void QMenu::aboutToHide ( )
signal

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

Since
4.2
See also
aboutToShow(), hide()

Referenced by hideEvent().

◆ aboutToShow

void QMenu::aboutToShow ( )
signal

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

See also
aboutToHide(), show()

Referenced by popup().

◆ actionAt()

QAction * QMenu::actionAt ( const QPoint pt) const

Returns the item at pt; returns 0 if there is no item there.

Definition at line 1787 of file qmenu.cpp.

Referenced by QAccessibleMenu::childAt().

1788 {
1789  if (QAction *ret = d_func()->actionAt(pt))
1790  return ret;
1791  return 0;
1792 }
QAction * actionAt(const QPoint &) const
Returns the item at pt; returns 0 if there is no item there.
Definition: qmenu.cpp:1787
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ actionEvent()

void QMenu::actionEvent ( QActionEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2969 of file qmenu.cpp.

Referenced by QTornOffMenu::actionEvent().

2970 {
2971  Q_D(QMenu);
2972  d->itemsDirty = 1;
2973  setAttribute(Qt::WA_Resized, false);
2974  if (d->tornPopup)
2975  d->tornPopup->syncWithMenu(this, e);
2976  if (e->type() == QEvent::ActionAdded) {
2977  if(!d->tornoff) {
2978  connect(e->action(), SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
2979  connect(e->action(), SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
2980  }
2981  if (QWidgetAction *wa = qobject_cast<QWidgetAction *>(e->action())) {
2982  QWidget *widget = wa->requestWidget(this);
2983  if (widget)
2984  d->widgetItems.insert(wa, widget);
2985  }
2986  } else if (e->type() == QEvent::ActionRemoved) {
2987  e->action()->disconnect(this);
2988  if (e->action() == d->currentAction)
2989  d->currentAction = 0;
2990  if (QWidgetAction *wa = qobject_cast<QWidgetAction *>(e->action())) {
2991  if (QWidget *widget = d->widgetItems.value(wa))
2992  wa->releaseWidget(widget);
2993  }
2994  d->widgetItems.remove(e->action());
2995  }
2996 
2997 #ifdef Q_WS_MAC
2998  if (d->mac_menu) {
2999  if (e->type() == QEvent::ActionAdded)
3000  d->mac_menu->addAction(e->action(), d->mac_menu->findAction(e->before()), d);
3001  else if (e->type() == QEvent::ActionRemoved)
3002  d->mac_menu->removeAction(e->action());
3003  else if (e->type() == QEvent::ActionChanged)
3004  d->mac_menu->syncAction(e->action());
3005  }
3006 #endif
3007 
3008 #if defined(Q_WS_WINCE) && !defined(QT_NO_MENUBAR)
3009  if (!d->wce_menu)
3010  d->wce_menu = new QMenuPrivate::QWceMenuPrivate;
3011  if (e->type() == QEvent::ActionAdded)
3012  d->wce_menu->addAction(e->action(), d->wce_menu->findAction(e->before()));
3013  else if (e->type() == QEvent::ActionRemoved)
3014  d->wce_menu->removeAction(e->action());
3015  else if (e->type() == QEvent::ActionChanged)
3016  d->wce_menu->syncAction(e->action());
3017 #endif
3018 
3019 #ifdef Q_WS_S60
3020  if (!d->symbian_menu)
3021  d->symbian_menu = new QMenuPrivate::QSymbianMenuPrivate;
3022  if (e->type() == QEvent::ActionAdded)
3023  d->symbian_menu->addAction(e->action(), d->symbian_menu->findAction(e->before()));
3024  else if (e->type() == QEvent::ActionRemoved)
3025  d->symbian_menu->removeAction(e->action());
3026  else if (e->type() == QEvent::ActionChanged)
3027  d->symbian_menu->syncAction(e->action());
3028 #endif
3029  if (isVisible()) {
3030  d->updateActionRects();
3031  resize(sizeHint());
3032  update();
3033  }
3034 }
QAction * before() const
If type() is ActionAdded , returns the action that should appear before action(). ...
Definition: qevent.h:640
double d
Definition: qnumeric_p.h:62
QPointer< QWidget > widget
void hovered(QAction *action)
This signal is emitted when a menu action is highlighted; action is the action that caused the signal...
bool isVisible() const
Definition: qwidget.h:1005
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QSize sizeHint() const
Reimplemented Function
Definition: qmenu.cpp:1805
#define Q_D(Class)
Definition: qglobal.h:2482
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void triggered(QAction *action)
This signal is emitted when an action in this menu is triggered.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
QAction * action() const
Returns the action that is changed, added, or removed.
Definition: qevent.h:639
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
The QWidgetAction class extends QAction by an interface for inserting custom widgets into action base...
Definition: qwidgetaction.h:57
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Definition: qwidget.h:1014
if(void) toggleToolbarShown
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
Type type() const
Returns the event type.
Definition: qcoreevent.h:303

◆ actionGeometry()

QRect QMenu::actionGeometry ( QAction act) const

Returns the geometry of action act.

Definition at line 1797 of file qmenu.cpp.

Referenced by QAccessibleMenu::rect().

1798 {
1799  return d_func()->actionRect(act);
1800 }

◆ activeAction()

QAction * QMenu::activeAction ( ) const

Returns the currently highlighted action, or 0 if no action is currently highlighted.

Definition at line 1720 of file qmenu.cpp.

Referenced by QMenuPrivate::hideMenu(), and QAccessibleMenu::state().

1721 {
1722  return d_func()->currentAction;
1723 }

◆ addAction() [1/4]

QAction * QMenu::addAction ( const QString text)

This convenience function creates a new action with text.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The function adds the newly created action to the menu's list of actions, and returns it.

See also
QWidget::addAction()

Definition at line 1453 of file qmenu.cpp.

Referenced by QFileDialogPrivate::_q_showContextMenu(), addAction(), addMenu(), addSeparator(), QMdiSubWindowPrivate::addToSystemMenu(), QDecoration::buildSysMenu(), QmlJSDebugger::ToolBarColorBox::contextMenuEvent(), QScrollBar::contextMenuEvent(), QDialog::contextMenuEvent(), QAbstractSpinBox::contextMenuEvent(), QmlJSDebugger::LiveSelectionTool::createContextMenu(), QCalendarWidgetPrivate::createNavigationBar(), QMainWindow::createPopupMenu(), QTextControl::createStandardContextMenu(), QLineEdit::createStandardContextMenu(), QScriptDebugger::createStandardMenu(), QScriptEdit::extraAreaMouseEvent(), QToolBarLayout::layoutActions(), menuItemEntry(), QmlJSDebugger::ZoomTool::mousePressEvent(), QToolButtonPrivate::popupTimerDone(), QmlJSDebugger::QmlToolBar::QmlToolBar(), QMultiInputContext::QMultiInputContext(), QTornOffMenu::QTornOffMenu(), QUnicodeControlCharacterMenu::QUnicodeControlCharacterMenu(), setSeparatorsCollapsible(), and QScriptEngineDebugger::standardWindow().

1454 {
1455  QAction *ret = new QAction(text, this);
1456  addAction(ret);
1457  return ret;
1458 }
friend class QAction
Definition: qmenu.h:415
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ addAction() [2/4]

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

This convenience function creates a new action with an icon and some text.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The function adds the newly created action to the menu's list of actions, and returns it.

See also
QWidget::addAction()

Definition at line 1472 of file qmenu.cpp.

1473 {
1474  QAction *ret = new QAction(icon, text, this);
1475  addAction(ret);
1476  return ret;
1477 }
friend class QAction
Definition: qmenu.h:415
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ addAction() [3/4]

QAction * QMenu::addAction ( const QString text,
const QObject receiver,
const char *  member,
const QKeySequence shortcut = 0 
)

This convenience function creates a new action with the text text and an optional shortcut shortcut.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

See also
QWidget::addAction()

Definition at line 1493 of file qmenu.cpp.

1494 {
1495  QAction *action = new QAction(text, this);
1496 #ifdef QT_NO_SHORTCUT
1497  Q_UNUSED(shortcut);
1498 #else
1499  action->setShortcut(shortcut);
1500 #endif
1501  QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
1502  addAction(action);
1503  return action;
1504 }
void setShortcut(const QKeySequence &shortcut)
Definition: qaction.cpp:450
friend class QAction
Definition: qmenu.h:415
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void triggered(QAction *action)
This signal is emitted when an action in this menu is triggered.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ addAction() [4/4]

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

This convenience function creates a new action with an icon and some text and an optional shortcut shortcut.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

See also
QWidget::addAction()

Definition at line 1520 of file qmenu.cpp.

1522 {
1523  QAction *action = new QAction(icon, text, this);
1524 #ifdef QT_NO_SHORTCUT
1525  Q_UNUSED(shortcut);
1526 #else
1527  action->setShortcut(shortcut);
1528 #endif
1529  QObject::connect(action, SIGNAL(triggered(bool)), receiver, member);
1530  addAction(action);
1531  return action;
1532 }
void setShortcut(const QKeySequence &shortcut)
Definition: qaction.cpp:450
friend class QAction
Definition: qmenu.h:415
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void triggered(QAction *action)
This signal is emitted when an action in this menu is triggered.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ addMenu() [1/3]

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() QMenu::menuAction()

Definition at line 1541 of file qmenu.cpp.

Referenced by QTextControl::createStandardContextMenu(), QLineEdit::createStandardContextMenu(), and menuItemEntry().

1542 {
1543  QAction *action = menu->menuAction();
1544  addAction(action);
1545  return action;
1546 }
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
QAction * menuAction() const
Returns the action associated with this menu.
Definition: qmenu.cpp:711
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ addMenu() [2/3]

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() QMenu::menuAction()

Definition at line 1554 of file qmenu.cpp.

1555 {
1556  QMenu *menu = new QMenu(title, this);
1557  addAction(menu->menuAction());
1558  return menu;
1559 }
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
QMenu(QWidget *parent=0)
Constructs a menu with parent parent.
Definition: qmenu.cpp:1386
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
QAction * menuAction() const
Returns the action associated with this menu.
Definition: qmenu.cpp:711

◆ addMenu() [3/3]

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() QMenu::menuAction()

Definition at line 1567 of file qmenu.cpp.

1568 {
1569  QMenu *menu = new QMenu(title, this);
1570  menu->setIcon(icon);
1571  addAction(menu->menuAction());
1572  return menu;
1573 }
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
QMenu(QWidget *parent=0)
Constructs a menu with parent parent.
Definition: qmenu.cpp:1386
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void setIcon(const QIcon &icon)
Definition: qmenu.cpp:754
QAction * menuAction() const
Returns the action associated with this menu.
Definition: qmenu.cpp:711

◆ addSeparator()

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.

See also
QWidget::addAction()

Definition at line 1583 of file qmenu.cpp.

Referenced by QFileDialogPrivate::_q_showContextMenu(), QDecoration::buildSysMenu(), QScrollBar::contextMenuEvent(), QAbstractSpinBox::contextMenuEvent(), QmlJSDebugger::LiveSelectionTool::createContextMenu(), QMainWindow::createPopupMenu(), QTextControl::createStandardContextMenu(), QLineEdit::createStandardContextMenu(), QScriptDebugger::createStandardMenu(), QMdiSubWindowPrivate::createSystemMenu(), QScriptEdit::extraAreaMouseEvent(), menuItemEntry(), QmlJSDebugger::ZoomTool::mousePressEvent(), and QScriptEngineDebugger::standardWindow().

1584 {
1585  QAction *action = new QAction(this);
1586  action->setSeparator(true);
1587  addAction(action);
1588  return action;
1589 }
friend class QAction
Definition: qmenu.h:415
void setSeparator(bool b)
If b is true then this action will be considered a separator.
Definition: qaction.cpp:823
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ changeEvent()

void QMenu::changeEvent ( QEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2414 of file qmenu.cpp.

2415 {
2416  Q_D(QMenu);
2417  if (e->type() == QEvent::StyleChange || e->type() == QEvent::FontChange ||
2419  d->itemsDirty = 1;
2421  if (isVisible())
2422  resize(sizeHint());
2423  if (!style()->styleHint(QStyle::SH_Menu_Scrollable, 0, this)) {
2424  delete d->scroll;
2425  d->scroll = 0;
2426  } else if (!d->scroll) {
2427  d->scroll = new QMenuPrivate::QMenuScroller;
2428  d->scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone;
2429  }
2430  } else if (e->type() == QEvent::EnabledChange) {
2431  if (d->tornPopup) // torn-off menu
2432  d->tornPopup->setEnabled(isEnabled());
2433  d->menuAction->setEnabled(isEnabled());
2434 #if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
2435  if (d->mac_menu)
2436  d->setMacMenuEnabled(isEnabled());
2437 #endif
2438  }
2440 }
double d
Definition: qnumeric_p.h:62
bool isVisible() const
Definition: qwidget.h:1005
QSize sizeHint() const
Reimplemented Function
Definition: qmenu.cpp:1805
#define Q_D(Class)
Definition: qglobal.h:2482
QStyle * style() const
Definition: qwidget.cpp:2742
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qwidget.cpp:9170
const char * styleHint(const QFontDef &request)
bool isEnabled() const
Definition: qwidget.h:948
void setMouseTracking(bool enable)
Definition: qwidget.h:990
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Definition: qwidget.h:1014
Type type() const
Returns the event type.
Definition: qcoreevent.h:303

◆ clear()

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()

Definition at line 1755 of file qmenu.cpp.

Referenced by QToolBarLayout::layoutActions(), and QMenuBarPrivate::updateGeometries().

1756 {
1757  QList<QAction*> acts = actions();
1758 
1759  for(int i = 0; i < acts.size(); i++) {
1760 #ifdef QT_SOFTKEYS_ENABLED
1761  Q_D(QMenu);
1762  // Lets not touch to our internal softkey actions
1763  if(acts[i] == d->selectAction || acts[i] == d->cancelAction)
1764  continue;
1765 #endif
1766  removeAction(acts[i]);
1767  if (acts[i]->parent() == this && acts[i]->d_func()->widgets.isEmpty())
1768  delete acts[i];
1769  }
1770 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void removeAction(QAction *action)
Removes the action action from this widget&#39;s list of actions.
Definition: qwidget.cpp:3386
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QList< QAction * > actions() const
Returns the (possibly empty) list of this widget&#39;s actions.
Definition: qwidget.cpp:3407
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ columnCount()

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

Definition at line 1779 of file qmenu.cpp.

1780 {
1781  return d_func()->ncols;
1782 }

◆ defaultAction()

QAction * QMenu::defaultAction ( ) const

Returns the current default action.

See also
setDefaultAction()

Definition at line 1637 of file qmenu.cpp.

1638 {
1639  return d_func()->defaultAction;
1640 }

◆ enterEvent()

void QMenu::enterEvent ( QEvent )
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2925 of file qmenu.cpp.

2926 {
2927  d_func()->motions = -1; // force us to ignore the generate mouse move in mouseMoveEvent()
2928 }

◆ event()

bool QMenu::event ( QEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2447 of file qmenu.cpp.

2448 {
2449  Q_D(QMenu);
2450  switch (e->type()) {
2451  case QEvent::Polish:
2452  d->updateLayoutDirection();
2453  break;
2454  case QEvent::ShortcutOverride: {
2455  QKeyEvent *kev = static_cast<QKeyEvent*>(e);
2456  if (kev->key() == Qt::Key_Up || kev->key() == Qt::Key_Down
2457  || kev->key() == Qt::Key_Left || kev->key() == Qt::Key_Right
2458  || kev->key() == Qt::Key_Enter || kev->key() == Qt::Key_Return
2459  || kev->key() == Qt::Key_Escape) {
2460  e->accept();
2461  return true;
2462  }
2463  }
2464  break;
2465  case QEvent::KeyPress: {
2466  QKeyEvent *ke = (QKeyEvent*)e;
2467  if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
2468  keyPressEvent(ke);
2469  return true;
2470  }
2471  } break;
2472  case QEvent::ContextMenu:
2473  if(d->menuDelayTimer.isActive()) {
2474  d->menuDelayTimer.stop();
2476  }
2477  break;
2478  case QEvent::Resize: {
2479  QStyleHintReturnMask menuMask;
2480  QStyleOption option;
2481  option.initFrom(this);
2482  if (style()->styleHint(QStyle::SH_Menu_Mask, &option, this, &menuMask)) {
2483  setMask(menuMask.region);
2484  }
2485  d->itemsDirty = 1;
2486  d->updateActionRects();
2487  break; }
2488  case QEvent::Show:
2489  d->mouseDown = 0;
2490  d->updateActionRects();
2491  if (d->currentAction)
2492  d->popupAction(d->currentAction, 0, false);
2493  break;
2494 #ifndef QT_NO_WHATSTHIS
2496  e->setAccepted(d->whatsThis.size());
2497  if (QAction *action = d->actionAt(static_cast<QHelpEvent*>(e)->pos())) {
2498  if (action->whatsThis().size() || action->menu())
2499  e->accept();
2500  }
2501  return true;
2502 #endif
2503 #ifdef QT_SOFTKEYS_ENABLED
2504  case QEvent::LanguageChange: {
2507  }
2508  break;
2509 #endif
2510  default:
2511  break;
2512  }
2513  return QWidget::event(e);
2514 }
double d
Definition: qnumeric_p.h:62
The QKeyEvent class describes a key event.
Definition: qevent.h:224
static QString standardSoftKeyText(StandardSoftKey standardKey)
void keyPressEvent(QKeyEvent *)
Reimplemented Function
Definition: qmenu.cpp:2530
void setAccepted(bool accepted)
Definition: qcoreevent.h:306
#define Q_D(Class)
Definition: qglobal.h:2482
QStyle * style() const
Definition: qwidget.cpp:2742
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
The QStyleOption class stores the parameters used by QStyle functions.
Definition: qstyleoption.h:67
void initFrom(const QWidget *w)
Definition: qstyleoption.h:99
const char * styleHint(const QFontDef &request)
void setMask(const QBitmap &)
Causes only the pixels of the widget for which bitmap has a corresponding 1 bit to be visible...
Definition: qwidget.cpp:13309
The QStyleHintReturnMask class provides style hints that return a QRegion.
Definition: qstyleoption.h:923
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void accept()
Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
Definition: qcoreevent.h:309
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
QRegion region
the region for style hints that return a QRegion
Definition: qstyleoption.h:930
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
void internalDelayedPopup()
Definition: qmenu.cpp:3048
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ exec() [1/4]

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 0 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:

exec(e->globalPos());

Definition at line 2101 of file qmenu.cpp.

Referenced by QPushButtonPrivate::_q_popupPressed(), QFileDialogPrivate::_q_showContextMenu(), QmlJSDebugger::ToolBarColorBox::contextMenuEvent(), QScrollBar::contextMenuEvent(), QDialog::contextMenuEvent(), QAbstractSpinBox::contextMenuEvent(), QMdiAreaTabBar::contextMenuEvent(), QmlJSDebugger::LiveSelectionTool::createContextMenu(), QWidget::event(), exec(), QScriptEdit::extraAreaMouseEvent(), QmlJSDebugger::ZoomTool::mousePressEvent(), QToolButtonPrivate::popupTimerDone(), and QSidebar::showContextMenu().

2102 {
2103  return exec(pos());
2104 }
QPoint pos() const
QAction * exec()
Executes this menu synchronously.
Definition: qmenu.cpp:2101

◆ exec() [2/4]

QAction * QMenu::exec ( const QPoint p,
QAction action = 0 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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 0 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:

exec(e->globalPos());

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(), QWidget::mapToGlobal()

Definition at line 2142 of file qmenu.cpp.

2143 {
2144  Q_D(QMenu);
2145  createWinId();
2146  QEventLoop eventLoop;
2147  d->eventLoop = &eventLoop;
2148  popup(p, action);
2149 
2150  QPointer<QObject> guard = this;
2151  (void) eventLoop.exec();
2152  if (guard.isNull())
2153  return 0;
2154 
2155  action = d->syncAction;
2156  d->syncAction = 0;
2157  d->eventLoop = 0;
2158  return action;
2159 }
double d
Definition: qnumeric_p.h:62
bool isNull() const
Returns true if the referenced object has been destroyed or if there is no referenced object; otherwi...
Definition: qpointer.h:70
void popup(const QPoint &pos, QAction *at=0)
Displays the menu so that the action atAction will be at the specified global position p...
Definition: qmenu.cpp:1847
#define Q_D(Class)
Definition: qglobal.h:2482
The QEventLoop class provides a means of entering and leaving an event loop.
Definition: qeventloop.h:55
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void createWinId()
Definition: qwidget.cpp:2626
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
Definition: qeventloop.cpp:181

◆ exec() [3/4]

QAction * QMenu::exec ( QList< QAction *>  actions,
const QPoint pos,
QAction at = 0 
)
static

Executes a menu synchronously.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

The function returns the triggered QAction in either the popup menu or one of its submenus, or 0 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
foreach (QAction *a, actions)
menu.addAction(a);
menu.exec(pos, at);
See also
popup(), QWidget::mapToGlobal()

Definition at line 2215 of file qmenu.cpp.

2216 {
2217  // ### Qt 5: merge
2218  return exec(actions, pos, at, 0);
2219 }
QAction * exec()
Executes this menu synchronously.
Definition: qmenu.cpp:2101

◆ exec() [4/4]

QAction * QMenu::exec ( QList< QAction *>  actions,
const QPoint pos,
QAction at,
QWidget parent 
)
static

Executes a menu synchronously.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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 0 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
foreach (QAction *a, actions)
menu.addAction(a);
menu.exec(pos, at);
See also
popup(), QWidget::mapToGlobal()

Definition at line 2186 of file qmenu.cpp.

2187 {
2188  QMenu menu(parent);
2189  menu.addActions(actions);
2190  return menu.exec(pos, at);
2191 }
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ focusNextPrevChild()

bool QMenu::focusNextPrevChild ( bool  next)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2519 of file qmenu.cpp.

2520 {
2521  setFocus();
2523  keyPressEvent(&ev);
2524  return true;
2525 }
The QKeyEvent class describes a key event.
Definition: qevent.h:224
void keyPressEvent(QKeyEvent *)
Reimplemented Function
Definition: qmenu.cpp:2530
void setFocus()
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its paren...
Definition: qwidget.h:432

◆ hideEvent()

void QMenu::hideEvent ( QHideEvent )
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2224 of file qmenu.cpp.

2225 {
2226  Q_D(QMenu);
2227  emit aboutToHide();
2228  if (d->eventLoop)
2229  d->eventLoop->exit();
2230  d->setCurrentAction(0);
2231 #ifndef QT_NO_ACCESSIBILITY
2233 #endif
2234 #ifndef QT_NO_MENUBAR
2235  if (QMenuBar *mb = qobject_cast<QMenuBar*>(d->causedPopup.widget))
2236  mb->d_func()->setCurrentAction(0);
2237 #endif
2238  d->mouseDown = 0;
2239  d->hasHadMouse = false;
2240  d->causedPopup.widget = 0;
2241  d->causedPopup.action = 0;
2242  if (d->scroll)
2243  d->scroll->scrollTimer.stop(); //make sure the timer stops
2244 }
double d
Definition: qnumeric_p.h:62
static void updateAccessibility(QObject *, int who, Event reason)
Notifies accessibility clients about a change in object&#39;s accessibility information.
#define Q_D(Class)
Definition: qglobal.h:2482
void aboutToHide()
This signal is emitted just before the menu is hidden from the user.
#define emit
Definition: qobjectdefs.h:76
The QMenuBar class provides a horizontal menu bar.
Definition: qmenubar.h:62
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ hideTearOffMenu()

void QMenu::hideTearOffMenu ( )

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

See also
isTearOffMenuVisible() isTearOffEnabled()

Definition at line 1697 of file qmenu.cpp.

Referenced by setTearOffEnabled(), and ~QMenu().

1698 {
1699  if (QWidget *w = d_func()->tornPopup)
1700  w->close();
1701 }
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150

◆ hovered

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.

Often this is used to update status information.

See also
triggered(), QAction::hovered()

Referenced by actionEvent().

◆ icon()

QIcon QMenu::icon ( ) const

◆ initStyleOption()

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 don't want to fill in all the information themselves.

See also
QStyleOption::initFrom() QMenuBar::initStyleOption()

Definition at line 1222 of file qmenu.cpp.

Referenced by paintEvent().

1223 {
1224  if (!option || !action)
1225  return;
1226 
1227  Q_D(const QMenu);
1228  option->initFrom(this);
1229  option->palette = palette();
1230  option->state = QStyle::State_None;
1231 
1232  if (window()->isActiveWindow())
1233  option->state |= QStyle::State_Active;
1234  if (isEnabled() && action->isEnabled()
1235  && (!action->menu() || action->menu()->isEnabled()))
1236  option->state |= QStyle::State_Enabled;
1237  else
1239 
1240  option->font = action->font().resolve(font());
1241  option->fontMetrics = QFontMetrics(option->font);
1242 
1243  if (d->currentAction && d->currentAction == action && !d->currentAction->isSeparator()) {
1244  option->state |= QStyle::State_Selected
1245  | (d->mouseDown ? QStyle::State_Sunken : QStyle::State_None);
1246  }
1247 
1248  option->menuHasCheckableItems = d->hasCheckableItems;
1249  if (!action->isCheckable()) {
1251  } else {
1252  option->checkType = (action->actionGroup() && action->actionGroup()->isExclusive())
1254  option->checked = action->isChecked();
1255  }
1256  if (action->menu())
1258  else if (action->isSeparator())
1260  else if (d->defaultAction == action)
1262  else
1264  if (action->isIconVisibleInMenu())
1265  option->icon = action->icon();
1266  QString textAndAccel = action->text();
1267 #ifndef QT_NO_SHORTCUT
1268  if (textAndAccel.indexOf(QLatin1Char('\t')) == -1) {
1269  QKeySequence seq = action->shortcut();
1270  if (!seq.isEmpty())
1271  textAndAccel += QLatin1Char('\t') + QString(seq);
1272  }
1273 #endif
1274  option->text = textAndAccel;
1275  option->tabWidth = d->tabWidth;
1276  option->maxIconWidth = d->maxIconWidth;
1277  option->menuRect = rect();
1278 }
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
Definition: qaction.cpp:839
QRect menuRect
the rectangle for the entire menu
Definition: qstyleoption.h:449
double d
Definition: qnumeric_p.h:62
bool isEnabled() const
Definition: qaction.cpp:1208
QKeySequence shortcut
the action&#39;s primary shortcut key
Definition: qaction.h:83
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
QIcon icon
the icon for the menu item
Definition: qstyleoption.h:451
QFont font
the font used for the menu item text
Definition: qstyleoption.h:454
friend class QFontMetrics
Definition: qwidget.h:749
QString text
the action&#39;s descriptive text
Definition: qaction.h:76
int maxIconWidth
the maximum icon width for the icon in the menu item
Definition: qstyleoption.h:452
bool isActiveWindow() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
QFontMetrics fontMetrics
the font metrics that should be used when drawing text in the control
Definition: qstyleoption.h:91
#define Q_D(Class)
Definition: qglobal.h:2482
QString text
the text for the menu item
Definition: qstyleoption.h:450
void setCurrentColorGroup(ColorGroup cg)
Set the palette&#39;s current color group to cg.
Definition: qpalette.h:105
QFont resolve(const QFont &) const
Returns a new QFont that has attributes copied from other that have not been previously set on this f...
Definition: qfont.cpp:1983
bool isIconVisibleInMenu() const
Definition: qaction.cpp:1651
void initFrom(const QWidget *w)
Definition: qstyleoption.h:99
bool checked
whether the menu item is checked or not
Definition: qstyleoption.h:447
const QPalette & palette() const
QFont font
the action&#39;s font
Definition: qaction.h:81
bool menuHasCheckableItems
whether the menu as a whole has checkable items or not
Definition: qstyleoption.h:448
QIcon icon
the action&#39;s icon
Definition: qaction.h:75
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
MenuItemType menuItemType
the type of menu item
Definition: qstyleoption.h:445
bool isEmpty() const
Returns true if the key sequence is empty; otherwise returns false.
QRect rect() const
bool isEnabled() const
Definition: qwidget.h:948
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
QMenu * menu() const
Returns the menu contained by this action.
Definition: qaction.cpp:793
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
int tabWidth
the tab width for the menu item
Definition: qstyleoption.h:453
bool isCheckable() const
Definition: qaction.cpp:1105
if(void) toggleToolbarShown
bool isChecked() const
Definition: qaction.cpp:1151
CheckType checkType
the type of checkmark of the menu item
Definition: qstyleoption.h:446
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
const QFont & font() const
QActionGroup * actionGroup() const
Returns the action group for this action.
Definition: qaction.cpp:747
bool isExclusive() const
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ insertMenu()

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

This convenience function inserts menu before action before and returns the menus menuAction().

See also
QWidget::insertAction(), addMenu()

Definition at line 1597 of file qmenu.cpp.

1598 {
1599  QAction *action = menu->menuAction();
1600  insertAction(before, action);
1601  return action;
1602 }
void insertAction(QAction *before, QAction *action)
Inserts the action action to this widget&#39;s list of actions, before the action before.
Definition: qwidget.cpp:3342
QAction * menuAction() const
Returns the action associated with this menu.
Definition: qmenu.cpp:711
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ insertSeparator()

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.

See also
QWidget::insertAction(), addSeparator()

Definition at line 1612 of file qmenu.cpp.

Referenced by setSeparatorsCollapsible().

1613 {
1614  QAction *action = new QAction(this);
1615  action->setSeparator(true);
1616  insertAction(before, action);
1617  return action;
1618 }
friend class QAction
Definition: qmenu.h:415
void setSeparator(bool b)
If b is true then this action will be considered a separator.
Definition: qaction.cpp:823
void insertAction(QAction *before, QAction *action)
Inserts the action action to this widget&#39;s list of actions, before the action before.
Definition: qwidget.cpp:3342
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ internalDelayedPopup

void QMenu::internalDelayedPopup ( )
privateslot
Warning
This function is not part of the public interface.

Definition at line 3048 of file qmenu.cpp.

Referenced by event(), and timerEvent().

3049 {
3050  Q_D(QMenu);
3051 
3052  //hide the current item
3053  if (QMenu *menu = d->activeMenu) {
3054  d->activeMenu = 0;
3055  d->hideMenu(menu);
3056  }
3057 
3058  if (!d->currentAction || !d->currentAction->isEnabled() || !d->currentAction->menu() ||
3059  !d->currentAction->menu()->isEnabled() || d->currentAction->menu()->isVisible())
3060  return;
3061 
3062  //setup
3063  d->activeMenu = d->currentAction->menu();
3064  d->activeMenu->d_func()->causedPopup.widget = this;
3065  d->activeMenu->d_func()->causedPopup.action = d->currentAction;
3066 
3067  int subMenuOffset = style()->pixelMetric(QStyle::PM_SubMenuOverlap, 0, this);
3068  const QRect actionRect(d->actionRect(d->currentAction));
3069  const QSize menuSize(d->activeMenu->sizeHint());
3070  const QPoint rightPos(mapToGlobal(QPoint(actionRect.right() + subMenuOffset + 1, actionRect.top())));
3071 
3072  QPoint pos(rightPos);
3073 
3074  //calc sloppy focus buffer
3075  if (style()->styleHint(QStyle::SH_Menu_SloppySubMenus, 0, this)) {
3076  QPoint cur = QCursor::pos();
3077  if (actionRect.contains(mapFromGlobal(cur))) {
3078  QPoint pts[4];
3079  pts[0] = QPoint(cur.x(), cur.y() - 2);
3080  pts[3] = QPoint(cur.x(), cur.y() + 2);
3081  if (pos.x() >= cur.x()) {
3082  pts[1] = QPoint(geometry().right(), pos.y());
3083  pts[2] = QPoint(geometry().right(), pos.y() + menuSize.height());
3084  } else {
3085  pts[1] = QPoint(pos.x() + menuSize.width(), pos.y());
3086  pts[2] = QPoint(pos.x() + menuSize.width(), pos.y() + menuSize.height());
3087  }
3088  QPolygon points(4);
3089  for(int i = 0; i < 4; i++)
3090  points.setPoint(i, mapFromGlobal(pts[i]));
3091  d->sloppyRegion = QRegion(points);
3092  }
3093  }
3094 
3095  //do the popup
3096  d->activeMenu->popup(pos);
3097 }
QPoint pos() const
double d
Definition: qnumeric_p.h:62
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
QStyle * style() const
Definition: qwidget.cpp:2742
const char * styleHint(const QFontDef &request)
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
QPoint mapFromGlobal(const QPoint &) const
Translates the global screen coordinate pos to widget coordinates.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
const QRect & geometry() const
QPoint mapToGlobal(const QPoint &) const
Translates the widget coordinate pos to global screen coordinates.
static QPoint pos()
Returns the position of the cursor (hot spot) in global screen coordinates.
Definition: qcursor_mac.mm:310

◆ internalSetSloppyAction

void QMenu::internalSetSloppyAction ( )
privateslot
Warning
This function is not part of the public interface.

Definition at line 3039 of file qmenu.cpp.

Referenced by timerEvent().

3040 {
3041  if (d_func()->sloppyAction)
3042  d_func()->setCurrentAction(d_func()->sloppyAction, 0);
3043 }

◆ isEmpty()

bool QMenu::isEmpty ( ) const

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

Since
4.2
See also
QWidget::actions()

Definition at line 1737 of file qmenu.cpp.

Referenced by QMainWindow::contextMenuEvent(), and QLineEdit::createStandardContextMenu().

1738 {
1739  bool ret = true;
1740  for(int i = 0; ret && i < actions().count(); ++i) {
1741  const QAction *action = actions().at(i);
1742  if (!action->isSeparator() && action->isVisible()) {
1743  ret = false;
1744  }
1745  }
1746  return ret;
1747 }
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
Definition: qaction.cpp:839
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool isVisible() const
Definition: qaction.cpp:1246
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QList< QAction * > actions() const
Returns the (possibly empty) list of this widget&#39;s actions.
Definition: qwidget.cpp:3407
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ isTearOffEnabled()

bool QMenu::isTearOffEnabled ( ) const

Definition at line 1672 of file qmenu.cpp.

1673 {
1674  return d_func()->tearoff;
1675 }

◆ isTearOffMenuVisible()

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()

Definition at line 1684 of file qmenu.cpp.

1685 {
1686  if (d_func()->tornPopup)
1687  return d_func()->tornPopup->isVisible();
1688  return false;
1689 }

◆ keyPressEvent()

void QMenu::keyPressEvent ( QKeyEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2530 of file qmenu.cpp.

Referenced by event(), and focusNextPrevChild().

2531 {
2532  Q_D(QMenu);
2533  d->updateActionRects();
2534  int key = e->key();
2535  if (isRightToLeft()) { // in reverse mode open/close key for submenues are reversed
2536  if (key == Qt::Key_Left)
2537  key = Qt::Key_Right;
2538  else if (key == Qt::Key_Right)
2539  key = Qt::Key_Left;
2540  }
2541 #ifndef Q_WS_MAC
2542  if (key == Qt::Key_Tab) //means down
2543  key = Qt::Key_Down;
2544  if (key == Qt::Key_Backtab) //means up
2545  key = Qt::Key_Up;
2546 #endif
2547 
2548  bool key_consumed = false;
2549  switch(key) {
2550  case Qt::Key_Home:
2551  key_consumed = true;
2552  if (d->scroll)
2553  d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollTop, true);
2554  break;
2555  case Qt::Key_End:
2556  key_consumed = true;
2557  if (d->scroll)
2558  d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollBottom, true);
2559  break;
2560  case Qt::Key_PageUp:
2561  key_consumed = true;
2562  if (d->currentAction && d->scroll) {
2563  if(d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)
2564  d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollUp, true, true);
2565  else
2566  d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollTop, true);
2567  }
2568  break;
2569  case Qt::Key_PageDown:
2570  key_consumed = true;
2571  if (d->currentAction && d->scroll) {
2572  if(d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown)
2573  d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollDown, true, true);
2574  else
2575  d->scrollMenu(QMenuPrivate::QMenuScroller::ScrollBottom, true);
2576  }
2577  break;
2578  case Qt::Key_Up:
2579  case Qt::Key_Down: {
2580  key_consumed = true;
2581  QAction *nextAction = 0;
2583  if (!d->currentAction) {
2584  if(key == Qt::Key_Down) {
2585  for(int i = 0; i < d->actions.count(); ++i) {
2586  QAction *act = d->actions.at(i);
2587  if (d->actionRects.at(i).isNull())
2588  continue;
2589  if (!act->isSeparator() &&
2591  || act->isEnabled())) {
2592  nextAction = act;
2593  break;
2594  }
2595  }
2596  } else {
2597  for(int i = d->actions.count()-1; i >= 0; --i) {
2598  QAction *act = d->actions.at(i);
2599  if (d->actionRects.at(i).isNull())
2600  continue;
2601  if (!act->isSeparator() &&
2603  || act->isEnabled())) {
2604  nextAction = act;
2605  break;
2606  }
2607  }
2608  }
2609  } else {
2610  for(int i = 0, y = 0; !nextAction && i < d->actions.count(); i++) {
2611  QAction *act = d->actions.at(i);
2612  if (act == d->currentAction) {
2613  if (key == Qt::Key_Up) {
2614  for(int next_i = i-1; true; next_i--) {
2615  if (next_i == -1) {
2617  break;
2618  if (d->scroll)
2620  next_i = d->actionRects.count()-1;
2621  }
2622  QAction *next = d->actions.at(next_i);
2623  if (next == d->currentAction)
2624  break;
2625  if (d->actionRects.at(next_i).isNull())
2626  continue;
2627  if (next->isSeparator() ||
2628  (!next->isEnabled() &&
2630  continue;
2631  nextAction = next;
2632  if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)) {
2633  int topVisible = d->scrollerHeight();
2634  if (d->tearoff)
2635  topVisible += style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this);
2636  if (((y + d->scroll->scrollOffset) - topVisible) <= d->actionRects.at(next_i).height())
2638  }
2639  break;
2640  }
2641  if (!nextAction && d->tearoff)
2642  d->tearoffHighlighted = 1;
2643  } else {
2644  y += d->actionRects.at(i).height();
2645  for(int next_i = i+1; true; next_i++) {
2646  if (next_i == d->actionRects.count()) {
2648  break;
2649  if (d->scroll)
2651  next_i = 0;
2652  }
2653  QAction *next = d->actions.at(next_i);
2654  if (next == d->currentAction)
2655  break;
2656  if (d->actionRects.at(next_i).isNull())
2657  continue;
2658  if (next->isSeparator() ||
2659  (!next->isEnabled() &&
2661  continue;
2662  nextAction = next;
2663  if (d->scroll && (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown)) {
2664  int bottomVisible = height() - d->scrollerHeight();
2665  if (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)
2666  bottomVisible -= d->scrollerHeight();
2667  if (d->tearoff)
2668  bottomVisible -= style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this);
2669  if ((y + d->scroll->scrollOffset + d->actionRects.at(next_i).height()) > bottomVisible)
2671  }
2672  break;
2673  }
2674  }
2675  break;
2676  }
2677  y += d->actionRects.at(i).height();
2678  }
2679  }
2680  if (nextAction) {
2681  if (d->scroll && scroll_loc != QMenuPrivate::QMenuScroller::ScrollStay) {
2682  d->scroll->scrollTimer.stop();
2683  d->scrollMenu(nextAction, scroll_loc);
2684  }
2685  d->setCurrentAction(nextAction, /*popup*/-1, QMenuPrivate::SelectedFromKeyboard);
2686  }
2687  break; }
2688 
2689  case Qt::Key_Right:
2690  if (d->currentAction && d->currentAction->isEnabled() && d->currentAction->menu()) {
2691  d->popupAction(d->currentAction, 0, true);
2692  key_consumed = true;
2693  break;
2694  }
2695  //FALL THROUGH
2696  case Qt::Key_Left: {
2697  if (d->currentAction && !d->scroll) {
2698  QAction *nextAction = 0;
2699  if (key == Qt::Key_Left) {
2700  QRect actionR = d->actionRect(d->currentAction);
2701  for(int x = actionR.left()-1; !nextAction && x >= 0; x--)
2702  nextAction = d->actionAt(QPoint(x, actionR.center().y()));
2703  } else {
2704  QRect actionR = d->actionRect(d->currentAction);
2705  for(int x = actionR.right()+1; !nextAction && x < width(); x++)
2706  nextAction = d->actionAt(QPoint(x, actionR.center().y()));
2707  }
2708  if (nextAction) {
2709  d->setCurrentAction(nextAction, /*popup*/-1, QMenuPrivate::SelectedFromKeyboard);
2710  key_consumed = true;
2711  }
2712  }
2713  if (!key_consumed && key == Qt::Key_Left && qobject_cast<QMenu*>(d->causedPopup.widget)) {
2714  QPointer<QWidget> caused = d->causedPopup.widget;
2715  d->hideMenu(this);
2716  if (caused)
2717  caused->setFocus();
2718  key_consumed = true;
2719  }
2720  break; }
2721 
2722  case Qt::Key_Alt:
2723  if (d->tornoff)
2724  break;
2725 
2726  key_consumed = true;
2728  {
2729  d->hideMenu(this);
2730 #ifndef QT_NO_MENUBAR
2731  if (QMenuBar *mb = qobject_cast<QMenuBar*>(QApplication::focusWidget())) {
2732  mb->d_func()->setKeyboardMode(false);
2733  }
2734 #endif
2735  }
2736  break;
2737 
2738  case Qt::Key_Escape:
2739 #ifdef QT_KEYPAD_NAVIGATION
2740  case Qt::Key_Back:
2741 #endif
2742  key_consumed = true;
2743  if (d->tornoff) {
2744  close();
2745  return;
2746  }
2747  {
2748  QPointer<QWidget> caused = d->causedPopup.widget;
2749  d->hideMenu(this); // hide after getting causedPopup
2750 #ifndef QT_NO_MENUBAR
2751  if (QMenuBar *mb = qobject_cast<QMenuBar*>(caused)) {
2752  mb->d_func()->setCurrentAction(d->menuAction);
2753  mb->d_func()->setKeyboardMode(true);
2754  }
2755 #endif
2756  }
2757  break;
2758 
2759  case Qt::Key_Space:
2761  break;
2762  // for motif, fall through
2763 #ifdef QT_KEYPAD_NAVIGATION
2764  case Qt::Key_Select:
2765 #endif
2766  case Qt::Key_Return:
2767  case Qt::Key_Enter: {
2768  if (!d->currentAction) {
2769  d->setFirstActionActive();
2770  key_consumed = true;
2771  break;
2772  }
2773 
2774  d->setSyncAction();
2775 
2776  if (d->currentAction->menu())
2777  d->popupAction(d->currentAction, 0, true);
2778  else
2779  d->activateAction(d->currentAction, QAction::Trigger);
2780  key_consumed = true;
2781  break; }
2782 
2783 #ifndef QT_NO_WHATSTHIS
2784  case Qt::Key_F1:
2785  if (!d->currentAction || d->currentAction->whatsThis().isNull())
2786  break;
2788  d->activateAction(d->currentAction, QAction::Trigger);
2789  return;
2790 #endif
2791  default:
2792  key_consumed = false;
2793  }
2794 
2795  if (!key_consumed) { // send to menu bar
2796  if ((!e->modifiers() || e->modifiers() == Qt::AltModifier || e->modifiers() == Qt::ShiftModifier) &&
2797  e->text().length()==1) {
2798  bool activateAction = false;
2799  QAction *nextAction = 0;
2800  if (style()->styleHint(QStyle::SH_Menu_KeyboardSearch, 0, this) && !e->modifiers()) {
2801  int best_match_count = 0;
2802  d->searchBufferTimer.start(2000, this);
2803  d->searchBuffer += e->text();
2804  for(int i = 0; i < d->actions.size(); ++i) {
2805  int match_count = 0;
2806  if (d->actionRects.at(i).isNull())
2807  continue;
2808  QAction *act = d->actions.at(i);
2809  const QString act_text = act->text();
2810  for(int c = 0; c < d->searchBuffer.size(); ++c) {
2811  if(act_text.indexOf(d->searchBuffer.at(c), 0, Qt::CaseInsensitive) != -1)
2812  ++match_count;
2813  }
2814  if(match_count > best_match_count) {
2815  best_match_count = match_count;
2816  nextAction = act;
2817  }
2818  }
2819  }
2820 #ifndef QT_NO_SHORTCUT
2821  else {
2822  int clashCount = 0;
2823  QAction *first = 0, *currentSelected = 0, *firstAfterCurrent = 0;
2824  QChar c = e->text().at(0).toUpper();
2825  for(int i = 0; i < d->actions.size(); ++i) {
2826  if (d->actionRects.at(i).isNull())
2827  continue;
2828  QAction *act = d->actions.at(i);
2829  QKeySequence sequence = QKeySequence::mnemonic(act->text());
2830  int key = sequence[0] & 0xffff;
2831  if (key == c.unicode()) {
2832  clashCount++;
2833  if (!first)
2834  first = act;
2835  if (act == d->currentAction)
2836  currentSelected = act;
2837  else if (!firstAfterCurrent && currentSelected)
2838  firstAfterCurrent = act;
2839  }
2840  }
2841  if (clashCount == 1)
2842  activateAction = true;
2843  if (clashCount >= 1) {
2844  if (clashCount == 1 || !currentSelected || !firstAfterCurrent)
2845  nextAction = first;
2846  else
2847  nextAction = firstAfterCurrent;
2848  }
2849  }
2850 #endif
2851  if (nextAction) {
2852  key_consumed = true;
2853  if(d->scroll)
2854  d->scrollMenu(nextAction, QMenuPrivate::QMenuScroller::ScrollCenter, false);
2855  d->setCurrentAction(nextAction, 0, QMenuPrivate::SelectedFromElsewhere, true);
2856  if (!nextAction->menu() && activateAction) {
2857  d->setSyncAction();
2858  d->activateAction(nextAction, QAction::Trigger);
2859  }
2860  }
2861  }
2862  if (!key_consumed) {
2863 #ifndef QT_NO_MENUBAR
2864  if (QMenuBar *mb = qobject_cast<QMenuBar*>(d->topCausedWidget())) {
2865  QAction *oldAct = mb->d_func()->currentAction;
2866  QApplication::sendEvent(mb, e);
2867  if (mb->d_func()->currentAction != oldAct)
2868  key_consumed = true;
2869  }
2870 #endif
2871  }
2872 
2873 #ifdef Q_OS_WIN32
2874  if (key_consumed && (e->key() == Qt::Key_Control || e->key() == Qt::Key_Shift || e->key() == Qt::Key_Meta))
2876 #endif // Q_OS_WIN32
2877  }
2878  if (key_consumed)
2879  e->accept();
2880  else
2881  e->ignore();
2882 }
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
Definition: qaction.cpp:839
double d
Definition: qnumeric_p.h:62
bool isEnabled() const
Definition: qaction.cpp:1208
static void beep()
Sounds the bell, using the default volume and sound.
int y() const
unsigned char c[8]
Definition: qnumeric_p.h:62
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
static QKeySequence mnemonic(const QString &text)
Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics ...
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QString text
the action&#39;s descriptive text
Definition: qaction.h:76
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=0, const QWidget *widget=0, QStyleHintReturn *returnData=0) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
int height() const
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QStyle * style() const
Definition: qwidget.cpp:2742
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifier flags that existed immediately after the event occurred.
Definition: qevent.cpp:999
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
int width() const
const char * styleHint(const QFontDef &request)
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QChar toUpper() const
Returns the uppercase equivalent if the character is lowercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1287
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
QPoint center() const
Returns the center point of the rectangle.
Definition: qrect.h:300
void setFocus()
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its paren...
Definition: qwidget.h:432
QMenu * menu() const
Returns the menu contained by this action.
Definition: qaction.cpp:793
The QMenuBar class provides a horizontal menu bar.
Definition: qmenubar.h:62
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
int x() const
QString text() const
Returns the Unicode text that this key generated.
Definition: qevent.h:236
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
void ignore()
Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
Definition: qcoreevent.h:310
void accept()
Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
Definition: qcoreevent.h:309
bool isRightToLeft() const
Definition: qwidget.h:428
bool close()
Closes this widget.
Definition: qwidget.cpp:8305
static QWidget * focusWidget()
Returns the application widget that has the keyboard input focus, or 0 if no widget in this applicati...
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
static void enterWhatsThisMode()
This function switches the user interface into "What&#39;s This?" mode.
Definition: qwhatsthis.cpp:633

◆ leaveEvent()

void QMenu::leaveEvent ( QEvent )
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2933 of file qmenu.cpp.

2934 {
2935  Q_D(QMenu);
2936  d->sloppyAction = 0;
2937  if (!d->sloppyRegion.isEmpty())
2938  d->sloppyRegion = QRegion();
2939  if (!d->activeMenu && d->currentAction)
2940  setActiveAction(0);
2941 }
void setActiveAction(QAction *act)
Sets the currently highlighted action to act.
Definition: qmenu.cpp:1707
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ macMenu()

OSMenuRef QMenu::macMenu ( OSMenuRef  merge = 0)
Warning
This function is not part of the public interface.

This function will return the OSMenuRef used to create the native menu bar bindings.

If Qt is built against Carbon, the OSMenuRef is a MenuRef that can be used with Carbon's Menu Manager API.

If Qt is built against Cocoa, the OSMenuRef is a NSMenu pointer.

Warning
This function is not portable.
See also
QMenuBar::macMenu()

Definition at line 1610 of file qmenu_mac.mm.

Referenced by actualMenuItemVisibility(), qt_mac_set_dock_menu(), QMenuBarPrivate::QMacMenuBarPrivate::syncAction(), and QMenuPrivate::QMacMenuPrivate::syncAction().

1610 { return d_func()->macMenu(merge); }

◆ menuAction()

QAction * QMenu::menuAction ( ) const

Returns the action associated with this menu.

Definition at line 711 of file qmenu.cpp.

Referenced by QMultiInputContext::actions(), QMenuBar::addMenu(), addMenu(), QMenuBar::defaultAction(), QMenuBar::insertMenu(), insertMenu(), QToolButton::setMenu(), setSeparatorsCollapsible(), and waitForPopup().

712 {
713  return d_func()->menuAction;
714 }

◆ mouseMoveEvent()

void QMenu::mouseMoveEvent ( QMouseEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2887 of file qmenu.cpp.

2888 {
2889  Q_D(QMenu);
2890  if (!isVisible() || d->aboutToHide || d->mouseEventTaken(e))
2891  return;
2892  d->motions++;
2893  if (d->motions == 0) // ignore first mouse move event (see enterEvent())
2894  return;
2895  d->hasHadMouse = d->hasHadMouse || rect().contains(e->pos());
2896 
2897  QAction *action = d->actionAt(e->pos());
2898  if (!action || action->isSeparator()) {
2899  if (d->hasHadMouse
2900  && d->sloppyDelayTimer == 0 // Keep things as they are while we're moving to the submenu
2901  && (!d->currentAction || (action && action->isSeparator())
2902  || !(d->currentAction->menu() && d->currentAction->menu()->isVisible())))
2903  d->setCurrentAction(0);
2904  return;
2905  } else if(e->buttons()) {
2906  d->mouseDown = this;
2907  }
2908  if (d->sloppyRegion.contains(e->pos())) {
2909  if (d->sloppyAction != action && d->sloppyDelayTimer != 0) {
2910  killTimer(d->sloppyDelayTimer);
2911  d->sloppyDelayTimer = 0;
2912  }
2913  if (d->sloppyDelayTimer == 0) {
2914  d->sloppyAction = action;
2915  d->sloppyDelayTimer = startTimer(style()->styleHint(QStyle::SH_Menu_SubMenuPopupDelay, 0, this)*6);
2916  }
2917  } else if (action != d->currentAction) {
2918  d->setCurrentAction(action, style()->styleHint(QStyle::SH_Menu_SubMenuPopupDelay, 0, this));
2919  }
2920 }
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
Definition: qaction.cpp:839
int startTimer(int interval)
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer...
Definition: qobject.cpp:1623
double d
Definition: qnumeric_p.h:62
bool isVisible() const
Definition: qwidget.h:1005
#define Q_D(Class)
Definition: qglobal.h:2482
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
QStyle * style() const
Definition: qwidget.cpp:2742
const char * styleHint(const QFontDef &request)
QRect rect() const
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

◆ mousePressEvent()

void QMenu::mousePressEvent ( QMouseEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2360 of file qmenu.cpp.

2361 {
2362  Q_D(QMenu);
2363  if (d->aboutToHide || d->mouseEventTaken(e))
2364  return;
2365  if (!rect().contains(e->pos())) {
2366  if (d->noReplayFor
2367  && QRect(d->noReplayFor->mapToGlobal(QPoint()), d->noReplayFor->size()).contains(e->globalPos()))
2369  if (d->eventLoop) // synchronous operation
2370  d->syncAction = 0;
2371  d->hideUpToMenuBar();
2372  return;
2373  }
2374  d->mouseDown = this;
2375 
2376  QAction *action = d->actionAt(e->pos());
2377  d->setCurrentAction(action, 20);
2378  update();
2379 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
QRect rect() const
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
const QPoint & globalPos() const
Returns the global position of the mouse cursor at the time of the event.
Definition: qevent.h:96
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ mouseReleaseEvent()

void QMenu::mouseReleaseEvent ( QMouseEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2384 of file qmenu.cpp.

2385 {
2386  Q_D(QMenu);
2387  if (d->aboutToHide || d->mouseEventTaken(e))
2388  return;
2389  if(d->mouseDown != this) {
2390  d->mouseDown = 0;
2391  return;
2392  }
2393 
2394  d->mouseDown = 0;
2395  d->setSyncAction();
2396  QAction *action = d->actionAt(e->pos());
2397 
2398  if (action && action == d->currentAction) {
2399  if (!action->menu()){
2400 #if defined(Q_WS_WIN)
2401  //On Windows only context menus can be activated with the right button
2402  if (e->button() == Qt::LeftButton || d->topCausedWidget() == 0)
2403 #endif
2404  d->activateAction(action, QAction::Trigger);
2405  }
2406  } else if (d->hasMouseMoved(e->globalPos())) {
2407  d->hideUpToMenuBar();
2408  }
2409 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
Qt::MouseButton button() const
Returns the button that caused the event.
Definition: qevent.h:101
QMenu * menu() const
Returns the menu contained by this action.
Definition: qaction.cpp:793
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
const QPoint & globalPos() const
Returns the global position of the mouse cursor at the time of the event.
Definition: qevent.h:96
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ paintEvent()

void QMenu::paintEvent ( QPaintEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2249 of file qmenu.cpp.

2250 {
2251  Q_D(QMenu);
2252  d->updateActionRects();
2253  QPainter p(this);
2254  QRegion emptyArea = QRegion(rect());
2255 
2256  QStyleOptionMenuItem menuOpt;
2257  menuOpt.initFrom(this);
2258  menuOpt.state = QStyle::State_None;
2260  menuOpt.maxIconWidth = 0;
2261  menuOpt.tabWidth = 0;
2262  style()->drawPrimitive(QStyle::PE_PanelMenu, &menuOpt, &p, this);
2263 
2264  //draw the items that need updating..
2265  for (int i = 0; i < d->actions.count(); ++i) {
2266  QAction *action = d->actions.at(i);
2267  QRect adjustedActionRect = d->actionRects.at(i);
2268  if (!e->rect().intersects(adjustedActionRect)
2269  || d->widgetItems.value(action))
2270  continue;
2271  //set the clip region to be extra safe (and adjust for the scrollers)
2272  QRegion adjustedActionReg(adjustedActionRect);
2273  emptyArea -= adjustedActionReg;
2274  p.setClipRegion(adjustedActionReg);
2275 
2277  initStyleOption(&opt, action);
2278  opt.rect = adjustedActionRect;
2279  style()->drawControl(QStyle::CE_MenuItem, &opt, &p, this);
2280  }
2281 
2282  const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, 0, this);
2283  //draw the scroller regions..
2284  if (d->scroll) {
2286  menuOpt.state |= QStyle::State_Enabled;
2287  if (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp) {
2288  menuOpt.rect.setRect(fw, fw, width() - (fw * 2), d->scrollerHeight());
2289  emptyArea -= QRegion(menuOpt.rect);
2290  p.setClipRect(menuOpt.rect);
2291  style()->drawControl(QStyle::CE_MenuScroller, &menuOpt, &p, this);
2292  }
2293  if (d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollDown) {
2294  menuOpt.rect.setRect(fw, height() - d->scrollerHeight() - fw, width() - (fw * 2),
2295  d->scrollerHeight());
2296  emptyArea -= QRegion(menuOpt.rect);
2297  menuOpt.state |= QStyle::State_DownArrow;
2298  p.setClipRect(menuOpt.rect);
2299  style()->drawControl(QStyle::CE_MenuScroller, &menuOpt, &p, this);
2300  }
2301  }
2302  //paint the tear off..
2303  if (d->tearoff) {
2305  menuOpt.rect.setRect(fw, fw, width() - (fw * 2),
2306  style()->pixelMetric(QStyle::PM_MenuTearoffHeight, 0, this));
2307  if (d->scroll && d->scroll->scrollFlags & QMenuPrivate::QMenuScroller::ScrollUp)
2308  menuOpt.rect.translate(0, d->scrollerHeight());
2309  emptyArea -= QRegion(menuOpt.rect);
2310  p.setClipRect(menuOpt.rect);
2311  menuOpt.state = QStyle::State_None;
2312  if (d->tearoffHighlighted)
2313  menuOpt.state |= QStyle::State_Selected;
2314  style()->drawControl(QStyle::CE_MenuTearoff, &menuOpt, &p, this);
2315  }
2316  //draw border
2317  if (fw) {
2318  QRegion borderReg;
2319  borderReg += QRect(0, 0, fw, height()); //left
2320  borderReg += QRect(width()-fw, 0, fw, height()); //right
2321  borderReg += QRect(0, 0, width(), fw); //top
2322  borderReg += QRect(0, height()-fw, width(), fw); //bottom
2323  p.setClipRegion(borderReg);
2324  emptyArea -= borderReg;
2325  QStyleOptionFrame frame;
2326  frame.rect = rect();
2327  frame.palette = palette();
2328  frame.state = QStyle::State_None;
2330  frame.midLineWidth = 0;
2331  style()->drawPrimitive(QStyle::PE_FrameMenu, &frame, &p, this);
2332  }
2333 
2334  //finally the rest of the space
2335  p.setClipRegion(emptyArea);
2336  menuOpt.state = QStyle::State_None;
2339  menuOpt.rect = rect();
2340  menuOpt.menuRect = rect();
2341  style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this);
2342 }
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
QRect menuRect
the rectangle for the entire menu
Definition: qstyleoption.h:449
double d
Definition: qnumeric_p.h:62
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
The QStyleOptionMenuItem class is used to describe the parameter necessary for drawing a menu item...
Definition: qstyleoption.h:435
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
int maxIconWidth
the maximum icon width for the icon in the menu item
Definition: qstyleoption.h:452
#define Q_D(Class)
Definition: qglobal.h:2482
int height() const
QStyle * style() const
Definition: qwidget.cpp:2742
int lineWidth
the line width for drawing the frame
Definition: qstyleoption.h:124
int width() const
void initFrom(const QWidget *w)
Definition: qstyleoption.h:99
const QPalette & palette() const
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
The QStyleOptionFrame class is used to describe the parameters for drawing a frame.
Definition: qstyleoption.h:118
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
MenuItemType menuItemType
the type of menu item
Definition: qstyleoption.h:445
QRect rect() const
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
int midLineWidth
the mid-line width for drawing the frame
Definition: qstyleoption.h:125
int tabWidth
the tab width for the menu item
Definition: qstyleoption.h:453
void setRect(int x, int y, int w, int h)
Sets the coordinates of the rectangle&#39;s top-left corner to ({x}, {y}), and its size to the given widt...
Definition: qrect.h:400
virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given element with the provided painter with the style options specified by option...
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
CheckType checkType
the type of checkmark of the menu item
Definition: qstyleoption.h:446
bool intersects(const QRect &r) const
Returns true if this rectangle intersects with the given rectangle (i.
Definition: qrect.cpp:1429
void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const
Initialize option with the values from this menu and information from action.
Definition: qmenu.cpp:1222
const QRect & rect() const
Returns the rectangle that needs to be updated.
Definition: qevent.h:305
void translate(int dx, int dy)
Moves the rectangle dx along the x axis and dy along the y axis, relative to the current position...
Definition: qrect.h:312
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64

◆ popup()

void QMenu::popup ( const QPoint p,
QAction atAction = 0 
)

Displays the menu so that the action atAction will be at the specified global position p.

Use popup() on the relevant QAction instead.

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(), exec()

Definition at line 1847 of file qmenu.cpp.

Referenced by QMessageBoxDetailsText::TextEdit::contextMenuEvent(), QLabel::contextMenuEvent(), QTextControlPrivate::contextMenuEvent(), QMainWindow::contextMenuEvent(), exec(), QSystemTrayIconSys::mousePressEvent(), setSeparatorsCollapsible(), and QSystemTrayIconSys::winEvent().

1848 {
1849  Q_D(QMenu);
1850 #ifndef Q_OS_SYMBIAN
1851  if (d->scroll) { // reset scroll state from last popup
1852  if (d->scroll->scrollOffset)
1853  d->itemsDirty = 1; // sizeHint will be incorrect if there is previous scroll
1854  d->scroll->scrollOffset = 0;
1855  d->scroll->scrollFlags = QMenuPrivate::QMenuScroller::ScrollNone;
1856  }
1857 #endif
1858  d->tearoffHighlighted = 0;
1859  d->motions = 0;
1860  d->doChildEffects = true;
1861  d->updateLayoutDirection();
1862 
1863 #ifndef QT_NO_MENUBAR
1864  // if this menu is part of a chain attached to a QMenuBar, set the
1865  // _NET_WM_WINDOW_TYPE_DROPDOWN_MENU X11 window type
1866  setAttribute(Qt::WA_X11NetWmWindowTypeDropDownMenu, qobject_cast<QMenuBar *>(d->topCausedWidget()) != 0);
1867 #endif
1868 
1869  ensurePolished(); // Get the right font
1870  emit aboutToShow();
1871  const bool actionListChanged = d->itemsDirty;
1872  d->updateActionRects();
1873  QPoint pos;
1874  QPushButton *causedButton = qobject_cast<QPushButton*>(d->causedPopup.widget);
1875  if (actionListChanged && causedButton)
1876  pos = QPushButtonPrivate::get(causedButton)->adjustedMenuPosition();
1877  else
1878  pos = p;
1879 
1880  QSize size = sizeHint();
1881  QRect screen;
1882 #ifndef QT_NO_GRAPHICSVIEW
1883  bool isEmbedded = !bypassGraphicsProxyWidget(this) && d->nearestGraphicsProxyWidget(this);
1884  if (isEmbedded)
1885  screen = d->popupGeometry(this);
1886  else
1887 #endif
1888  screen = d->popupGeometry(QApplication::desktop()->screenNumber(p));
1889  const int desktopFrame = style()->pixelMetric(QStyle::PM_MenuDesktopFrameWidth, 0, this);
1890  bool adjustToDesktop = !window()->testAttribute(Qt::WA_DontShowOnScreen);
1891  if (!d->scroll) {
1892  // if the screens have very different geometries and the menu is too big, we have to recalculate
1893  if (size.height() > screen.height() || size.width() > screen.width()) {
1894  size = d->adjustMenuSizeForScreen(screen);
1895  adjustToDesktop = true;
1896  }
1897  // Layout is not right, we might be able to save horizontal space
1898  if (d->ncols >1 && size.height() < screen.height()) {
1899  size = d->adjustMenuSizeForScreen(screen);
1900  adjustToDesktop = true;
1901  }
1902  }
1903 
1904 #ifdef QT_KEYPAD_NAVIGATION
1905  if (!atAction && QApplication::keypadNavigationEnabled()) {
1906  // Try to have one item activated
1907  if (d->defaultAction && d->defaultAction->isEnabled()) {
1908  atAction = d->defaultAction;
1909  // TODO: This works for first level menus, not yet sub menus
1910  } else {
1911  foreach (QAction *action, d->actions)
1912  if (action->isEnabled()) {
1913  atAction = action;
1914  break;
1915  }
1916  }
1917  d->currentAction = atAction;
1918  }
1919 #endif
1920  if (d->ncols > 1) {
1921  pos.setY(screen.top() + desktopFrame);
1922  } else if (atAction) {
1923  for (int i = 0, above_height = 0; i < d->actions.count(); i++) {
1924  QAction *action = d->actions.at(i);
1925  if (action == atAction) {
1926  int newY = pos.y() - above_height;
1927  if (d->scroll && newY < desktopFrame) {
1928  d->scroll->scrollFlags = d->scroll->scrollFlags
1930  d->scroll->scrollOffset = newY;
1931  newY = desktopFrame;
1932  }
1933  pos.setY(newY);
1934 
1935  if (d->scroll && d->scroll->scrollFlags != QMenuPrivate::QMenuScroller::ScrollNone
1937  int below_height = above_height + d->scroll->scrollOffset;
1938  for (int i2 = i; i2 < d->actionRects.count(); i2++)
1939  below_height += d->actionRects.at(i2).height();
1940  size.setHeight(below_height);
1941  }
1942  break;
1943  } else {
1944  above_height += d->actionRects.at(i).height();
1945  }
1946  }
1947  }
1948 
1949  QPoint mouse = QCursor::pos();
1950  d->mousePopupPos = mouse;
1951  const bool snapToMouse = !d->causedPopup.widget && (QRect(p.x() - 3, p.y() - 3, 6, 6).contains(mouse));
1952 
1953  const QSize menuSize(sizeHint());
1954  if (adjustToDesktop) {
1955  // handle popup falling "off screen"
1956  if (isRightToLeft()) {
1957  if (snapToMouse) // position flowing left from the mouse
1958  pos.setX(mouse.x() - size.width());
1959 
1960 #ifndef QT_NO_MENUBAR
1961  // if in a menubar, it should be right-aligned
1962  if (qobject_cast<QMenuBar*>(d->causedPopup.widget))
1963  pos.rx() -= size.width();
1964 #endif //QT_NO_MENUBAR
1965 
1966  if (pos.x() < screen.left() + desktopFrame)
1967  pos.setX(qMax(p.x(), screen.left() + desktopFrame));
1968  if (pos.x() + size.width() - 1 > screen.right() - desktopFrame)
1969  pos.setX(qMax(p.x() - size.width(), screen.right() - desktopFrame - size.width() + 1));
1970  } else {
1971  if (pos.x() + size.width() - 1 > screen.right() - desktopFrame)
1972  pos.setX(screen.right() - desktopFrame - size.width() + 1);
1973  if (pos.x() < screen.left() + desktopFrame)
1974  pos.setX(screen.left() + desktopFrame);
1975  }
1976  if (pos.y() + size.height() - 1 > screen.bottom() - desktopFrame) {
1977  if(snapToMouse)
1978  pos.setY(qMin(mouse.y() - (size.height() + desktopFrame), screen.bottom()-desktopFrame-size.height()+1));
1979  else
1980  pos.setY(qMax(p.y() - (size.height() + desktopFrame), screen.bottom()-desktopFrame-size.height()+1));
1981  } else if (pos.y() < screen.top() + desktopFrame) {
1982  pos.setY(screen.top() + desktopFrame);
1983  }
1984 
1985  if (pos.y() < screen.top() + desktopFrame)
1986  pos.setY(screen.top() + desktopFrame);
1987  if (pos.y() + menuSize.height() - 1 > screen.bottom() - desktopFrame) {
1988  if (d->scroll) {
1989  d->scroll->scrollFlags |= uint(QMenuPrivate::QMenuScroller::ScrollDown);
1990  int y = qMax(screen.y(),pos.y());
1991  size.setHeight(screen.bottom() - (desktopFrame * 2) - y);
1992  } else {
1993  // Too big for screen, bias to see bottom of menu (for some reason)
1994  pos.setY(screen.bottom() - size.height() + 1);
1995  }
1996  }
1997  }
1998  const int subMenuOffset = style()->pixelMetric(QStyle::PM_SubMenuOverlap, 0, this);
1999  QMenu *caused = qobject_cast<QMenu*>(d_func()->causedPopup.widget);
2000  if (caused && caused->geometry().width() + menuSize.width() + subMenuOffset < screen.width()) {
2001  QRect parentActionRect(caused->d_func()->actionRect(caused->d_func()->currentAction));
2002  const QPoint actionTopLeft = caused->mapToGlobal(parentActionRect.topLeft());
2003  parentActionRect.moveTopLeft(actionTopLeft);
2004  if (isRightToLeft()) {
2005  if ((pos.x() + menuSize.width() > parentActionRect.left() - subMenuOffset)
2006  && (pos.x() < parentActionRect.right()))
2007  {
2008  pos.rx() = parentActionRect.left() - menuSize.width();
2009  if (pos.x() < screen.x())
2010  pos.rx() = parentActionRect.right();
2011  if (pos.x() + menuSize.width() > screen.x() + screen.width())
2012  pos.rx() = screen.x();
2013  }
2014  } else {
2015  if ((pos.x() < parentActionRect.right() + subMenuOffset)
2016  && (pos.x() + menuSize.width() > parentActionRect.left()))
2017  {
2018  pos.rx() = parentActionRect.right();
2019  if (pos.x() + menuSize.width() > screen.x() + screen.width())
2020  pos.rx() = parentActionRect.left() - menuSize.width();
2021  if (pos.x() < screen.x())
2022  pos.rx() = screen.x() + screen.width() - menuSize.width();
2023  }
2024  }
2025  }
2026  setGeometry(QRect(pos, size));
2027 #ifndef QT_NO_EFFECTS
2029  int vGuess = QEffects::DownScroll;
2030  if (isRightToLeft()) {
2031  if ((snapToMouse && (pos.x() + size.width() / 2 > mouse.x())) ||
2032  (qobject_cast<QMenu*>(d->causedPopup.widget) && pos.x() + size.width() / 2 > d->causedPopup.widget->x()))
2033  hGuess = QEffects::RightScroll;
2034  } else {
2035  if ((snapToMouse && (pos.x() + size.width() / 2 < mouse.x())) ||
2036  (qobject_cast<QMenu*>(d->causedPopup.widget) && pos.x() + size.width() / 2 < d->causedPopup.widget->x()))
2037  hGuess = QEffects::LeftScroll;
2038  }
2039 
2040 #ifndef QT_NO_MENUBAR
2041  if ((snapToMouse && (pos.y() + size.height() / 2 < mouse.y())) ||
2042  (qobject_cast<QMenuBar*>(d->causedPopup.widget) &&
2043  pos.y() + size.width() / 2 < d->causedPopup.widget->mapToGlobal(d->causedPopup.widget->pos()).y()))
2044  vGuess = QEffects::UpScroll;
2045 #endif
2047  bool doChildEffects = true;
2048 #ifndef QT_NO_MENUBAR
2049  if (QMenuBar *mb = qobject_cast<QMenuBar*>(d->causedPopup.widget)) {
2050  doChildEffects = mb->d_func()->doChildEffects;
2051  mb->d_func()->doChildEffects = false;
2052  } else
2053 #endif
2054  if (QMenu *m = qobject_cast<QMenu*>(d->causedPopup.widget)) {
2055  doChildEffects = m->d_func()->doChildEffects;
2056  m->d_func()->doChildEffects = false;
2057  }
2058 
2059  if (doChildEffects) {
2061  qFadeEffect(this);
2062  else if (d->causedPopup.widget)
2063  qScrollEffect(this, qobject_cast<QMenu*>(d->causedPopup.widget) ? hGuess : vGuess);
2064  else
2065  qScrollEffect(this, hGuess | vGuess);
2066  } else {
2067  // kill any running effect
2068  qFadeEffect(0);
2069  qScrollEffect(0);
2070 
2071  show();
2072  }
2073  } else
2074 #endif
2075  {
2076  show();
2077  }
2078 
2079 #ifndef QT_NO_ACCESSIBILITY
2081 #endif
2082 }
QPoint pos() const
double d
Definition: qnumeric_p.h:62
static void updateAccessibility(QObject *, int who, Event reason)
Notifies accessibility clients about a change in object&#39;s accessibility information.
bool isEnabled() const
Definition: qaction.cpp:1208
static QPushButtonPrivate * get(QPushButton *b)
Definition: qpushbutton_p.h:71
int y() const
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QSize size() const
void ensurePolished() const
Ensures that the widget has been polished by QStyle (i.e., has a proper font and palette).
Definition: qwidget.cpp:10024
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
QSize sizeHint() const
Reimplemented Function
Definition: qmenu.cpp:1805
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
The QPushButton widget provides a command button.
Definition: qpushbutton.h:57
static bool bypassGraphicsProxyWidget(const QWidget *p)
Returns true if p or any of its parents enable the Qt::BypassGraphicsProxyWidget window flag...
Definition: qwidget_p.h:353
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
T * qobject_cast(QObject *object)
Definition: qobject.h:375
#define Q_D(Class)
Definition: qglobal.h:2482
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QStyle * style() const
Definition: qwidget.cpp:2742
QPoint adjustedMenuPosition()
int width() const
Returns the width.
Definition: qsize.h:126
void qScrollEffect(QWidget *w, QEffects::DirFlags orient, int time)
Scroll widget w in time ms.
Definition: qeffects.cpp:562
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
#define emit
Definition: qobjectdefs.h:76
const char * styleHint(const QFontDef &request)
int & rx()
Returns a reference to the x coordinate of this point.
Definition: qpoint.h:140
unsigned int uint
Definition: qglobal.h:996
void show()
Shows the widget and its child widgets.
static QDesktopWidget * desktop()
Returns the desktop widget (also called the root window).
void setY(int y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qpoint.h:137
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
void aboutToShow()
This signal is emitted just before the menu is shown to the user.
static bool isEffectEnabled(Qt::UIEffect)
Returns true if effect is enabled; otherwise returns false.
The QMenuBar class provides a horizontal menu bar.
Definition: qmenubar.h:62
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isRightToLeft() const
Definition: qwidget.h:428
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
const QRect & geometry() const
void qFadeEffect(QWidget *w, int time)
Fade in widget w in time ms.
Definition: qeffects.cpp:584
void setX(int x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qpoint.h:134
QPoint mapToGlobal(const QPoint &) const
Translates the widget coordinate pos to global screen coordinates.
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
static QPoint pos()
Returns the position of the cursor (hot spot) in global screen coordinates.
Definition: qcursor_mac.mm:310

◆ separatorsCollapsible()

bool QMenu::separatorsCollapsible ( ) const

Referenced by setNoReplayFor().

◆ setActiveAction()

void QMenu::setActiveAction ( QAction act)

Sets the currently highlighted action to act.

Definition at line 1707 of file qmenu.cpp.

Referenced by QAccessibleMenu::doAction(), QMenuPrivate::hideMenu(), and leaveEvent().

1708 {
1709  Q_D(QMenu);
1710  d->setCurrentAction(act, 0);
1711  if (d->scroll)
1712  d->scrollMenu(act, QMenuPrivate::QMenuScroller::ScrollCenter);
1713 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ setDefaultAction()

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()

Definition at line 1627 of file qmenu.cpp.

1628 {
1629  d_func()->defaultAction = act;
1630 }

◆ setIcon()

void QMenu::setIcon ( const QIcon icon)

Definition at line 754 of file qmenu.cpp.

Referenced by QMenuBar::addMenu(), and addMenu().

755 {
756  d_func()->menuAction->setIcon(icon);
757 }

◆ setNoReplayFor()

void QMenu::setNoReplayFor ( QWidget noReplayFor)
Warning
This function is not part of the public interface.

Definition at line 3164 of file qmenu.cpp.

Referenced by QPushButtonPrivate::_q_popupPressed(), and QToolButtonPrivate::popupTimerDone().

3165 {
3166 #ifdef Q_WS_WIN
3167  d_func()->noReplayFor = noReplayFor;
3168 #else
3169  Q_UNUSED(noReplayFor);
3170 #endif
3171 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ setSeparatorsCollapsible()

void QMenu::setSeparatorsCollapsible ( bool  collapse)

Definition at line 3194 of file qmenu.cpp.

3195 {
3196  Q_D(QMenu);
3197  if (d->collapsibleSeparators == collapse)
3198  return;
3199 
3200  d->collapsibleSeparators = collapse;
3201  d->itemsDirty = 1;
3202  if (isVisible()) {
3203  d->updateActionRects();
3204  update();
3205  }
3206 #ifdef Q_WS_MAC
3207  if (d->mac_menu)
3208  d->syncSeparatorsCollapsible(collapse);
3209 #endif
3210 }
double d
Definition: qnumeric_p.h:62
bool isVisible() const
Definition: qwidget.h:1005
#define Q_D(Class)
Definition: qglobal.h:2482
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ setTearOffEnabled()

void QMenu::setTearOffEnabled ( bool  b)

Definition at line 1658 of file qmenu.cpp.

1659 {
1660  Q_D(QMenu);
1661  if (d->tearoff == b)
1662  return;
1663  if (!b)
1664  hideTearOffMenu();
1665  d->tearoff = b;
1666 
1667  d->itemsDirty = true;
1668  if (isVisible())
1669  resize(sizeHint());
1670 }
double d
Definition: qnumeric_p.h:62
bool isVisible() const
Definition: qwidget.h:1005
QSize sizeHint() const
Reimplemented Function
Definition: qmenu.cpp:1805
#define Q_D(Class)
Definition: qglobal.h:2482
void hideTearOffMenu()
This function will forcibly hide the torn off menu making it disappear from the users desktop...
Definition: qmenu.cpp:1697
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Definition: qwidget.h:1014

◆ setTitle()

void QMenu::setTitle ( const QString title)

Definition at line 732 of file qmenu.cpp.

Referenced by QScriptDebugger::createStandardMenu(), menuItemEntry(), and QUnicodeControlCharacterMenu::QUnicodeControlCharacterMenu().

733 {
734  d_func()->menuAction->setText(text);
735 }
#define text
Definition: qobjectdefs.h:80

◆ sizeHint()

QSize QMenu::sizeHint ( ) const
virtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 1805 of file qmenu.cpp.

Referenced by QWorkspacePrivate::_q_showOperationMenu(), QTornOffMenu::actionEvent(), actionEvent(), QPushButtonPrivate::adjustedMenuPosition(), changeEvent(), popup(), QToolButtonPrivate::popupTimerDone(), and setTearOffEnabled().

1806 {
1807  Q_D(const QMenu);
1808  d->updateActionRects();
1809 
1810  QSize s;
1811  for (int i = 0; i < d->actionRects.count(); ++i) {
1812  const QRect &rect = d->actionRects.at(i);
1813  if (rect.isNull())
1814  continue;
1815  if (rect.bottom() >= s.height())
1816  s.setHeight(rect.y() + rect.height());
1817  if (rect.right() >= s.width())
1818  s.setWidth(rect.x() + rect.width());
1819  }
1820  // Note that the action rects calculated above already include
1821  // the top and left margins, so we only need to add margins for
1822  // the bottom and right.
1823  QStyleOption opt(0);
1824  opt.init(this);
1825  const int fw = style()->pixelMetric(QStyle::PM_MenuPanelWidth, &opt, this);
1826  s.rwidth() += style()->pixelMetric(QStyle::PM_MenuHMargin, &opt, this) + fw + d->rightmargin;
1827  s.rheight() += style()->pixelMetric(QStyle::PM_MenuVMargin, &opt, this) + fw + d->bottommargin;
1828 
1829  return style()->sizeFromContents(QStyle::CT_Menu, &opt,
1831 }
double d
Definition: qnumeric_p.h:62
bool isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition: qrect.h:231
static QSize globalStrut()
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
#define Q_D(Class)
Definition: qglobal.h:2482
QStyle * style() const
Definition: qwidget.cpp:2742
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
int width() const
Returns the width.
Definition: qsize.h:126
The QStyleOption class stores the parameters used by QStyle functions.
Definition: qstyleoption.h:67
QRect rect() const
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=0) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
int & rheight()
Returns a reference to the height.
Definition: qsize.h:144
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int & rwidth()
Returns a reference to the width.
Definition: qsize.h:141

◆ timerEvent()

void QMenu::timerEvent ( QTimerEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QObject.

Definition at line 2947 of file qmenu.cpp.

2948 {
2949  Q_D(QMenu);
2950  if (d->scroll && d->scroll->scrollTimer.timerId() == e->timerId()) {
2951  d->scrollMenu((QMenuPrivate::QMenuScroller::ScrollDirection)d->scroll->scrollDirection);
2952  if (d->scroll->scrollFlags == QMenuPrivate::QMenuScroller::ScrollNone)
2953  d->scroll->scrollTimer.stop();
2954  } else if(d->menuDelayTimer.timerId() == e->timerId()) {
2955  d->menuDelayTimer.stop();
2957  } else if (d->sloppyDelayTimer == e->timerId()) {
2958  killTimer(d->sloppyDelayTimer);
2959  d->sloppyDelayTimer = 0;
2961  } else if(d->searchBufferTimer.timerId() == e->timerId()) {
2962  d->searchBuffer.clear();
2963  }
2964 }
double d
Definition: qnumeric_p.h:62
void internalSetSloppyAction()
Definition: qmenu.cpp:3039
#define Q_D(Class)
Definition: qglobal.h:2482
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void internalDelayedPopup()
Definition: qmenu.cpp:3048
void killTimer(int id)
Kills the timer with timer identifier, id.
Definition: qobject.cpp:1650

◆ title()

QString QMenu::title ( ) const

Referenced by menuAction().

◆ triggered

void QMenu::triggered ( QAction action)
signal

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 QAction::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()

Referenced by actionEvent(), and addAction().

◆ wceCommands()

QAction * QMenu::wceCommands ( uint  command)
protected

This function sends native Windows CE commands to Qt menus.

Warning
This function is not part of the public interface.

Definition at line 297 of file qmenu_wince.cpp.

Referenced by QMenuPrivate::wceCommands(), and QMenuBarPrivate::wceEmitSignals().

298 {
299  Q_D(QMenu);
300  return d->wceCommands(command);
301 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

◆ wceMenu()

HMENU QMenu::wceMenu ( )

This function will return the HMENU used to create the native Windows CE menu bar bindings.

Warning
This function is not part of the public interface.

Definition at line 478 of file qmenu_wince.cpp.

Referenced by qt_wce_insert_action().

479 {
480  return d_func()->wceMenu();
481 }

◆ wheelEvent()

void QMenu::wheelEvent ( QWheelEvent e)
protectedvirtual

Reimplemented Function

Reimplemented from QWidget.

Definition at line 2348 of file qmenu.cpp.

2349 {
2350  Q_D(QMenu);
2351  if (d->scroll && rect().contains(e->pos()))
2352  d->scrollMenu(e->delta() > 0 ?
2354 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
int delta() const
Returns the distance that the wheel is rotated, in eighths of a degree.
Definition: qevent.h:150
QRect rect() const
const QPoint & pos() const
Returns the position of the mouse cursor relative to the widget that received the event...
Definition: qevent.h:151
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72

Friends and Related Functions

◆ Q3PopupMenu

friend class Q3PopupMenu
friend

Definition at line 413 of file qmenu.h.

◆ QAction

friend class QAction
friend

◆ QComboBox

friend class QComboBox
friend

Definition at line 414 of file qmenu.h.

◆ QMenuBar

friend class QMenuBar
friend

Definition at line 410 of file qmenu.h.

◆ QMenuBarPrivate

friend class QMenuBarPrivate
friend

Definition at line 411 of file qmenu.h.

◆ qt_mac_activate_action

bool qt_mac_activate_action ( OSMenuRef  menu,
uint  command,
QAction::ActionEvent  action_e,
bool  by_accel 
)
friend

Definition at line 209 of file qmenu_mac.mm.

210 {
211  //fire event
212  QMacMenuAction *action = 0;
213  if (GetMenuCommandProperty(menu, command, kMenuCreatorQt, kMenuPropertyQAction, sizeof(action), 0, &action) != noErr) {
214  QMenuMergeList *list = 0;
215  GetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyMergeList,
216  sizeof(list), 0, &list);
217  if (!list && qt_mac_current_menubar.qmenubar && qt_mac_current_menubar.qmenubar->isNativeMenuBar()) {
218  MenuRef apple_menu = qt_mac_current_menubar.qmenubar->d_func()->mac_menubar->apple_menu;
219  GetMenuItemProperty(apple_menu, 0, kMenuCreatorQt, kMenuPropertyMergeList, sizeof(list), 0, &list);
220  if (list)
221  menu = apple_menu;
222  }
223  if (list) {
224  for(int i = 0; i < list->size(); ++i) {
225  QMenuMergeItem item = list->at(i);
226  if (item.command == command && item.action) {
227  action = item.action;
228  break;
229  }
230  }
231  }
232  if (!action)
233  return false;
234  }
235 
236  if (action_e == QAction::Trigger && by_accel && action->ignore_accel) //no, not a real accel (ie tab)
237  return false;
238 
239  // Unhighlight the highlighted menu item before triggering the action to
240  // prevent items from staying highlighted while a modal dialog is shown.
241  // This also fixed the problem that parentless modal dialogs leave
242  // the menu item highlighted (since the menu bar is cleared for these types of dialogs).
243  if (action_e == QAction::Trigger)
244  HiliteMenu(0);
245 
246  action->action->activate(action_e);
247 
248  //now walk up firing for each "caused" widget (like in the platform independent menu)
249  QWidget *caused = 0;
250  if (action_e == QAction::Hover && GetMenuItemProperty(menu, 0, kMenuCreatorQt, kMenuPropertyCausedQWidget, sizeof(caused), 0, &caused) == noErr) {
251  MenuRef caused_menu = 0;
252  if (QMenu *qmenu2 = qobject_cast<QMenu*>(caused))
253  caused_menu = qmenu2->macMenu();
254  else if (QMenuBar *qmenubar2 = qobject_cast<QMenuBar*>(caused))
255  caused_menu = qmenubar2->macMenu();
256  else
257  caused_menu = 0;
258  while(caused_menu) {
259  //fire
260  QWidget *widget = 0;
261  GetMenuItemProperty(caused_menu, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(widget), 0, &widget);
262  if (QMenu *qmenu = qobject_cast<QMenu*>(widget)) {
263  action->action->showStatusText(widget);
264  emit qmenu->hovered(action->action);
265  } else if (QMenuBar *qmenubar = qobject_cast<QMenuBar*>(widget)) {
266  action->action->showStatusText(widget);
267  emit qmenubar->hovered(action->action);
268  break; //nothing more..
269  }
270 
271  //walk up
272  if (GetMenuItemProperty(caused_menu, 0, kMenuCreatorQt, kMenuPropertyCausedQWidget,
273  sizeof(caused), 0, &caused) != noErr)
274  break;
275  if (QMenu *qmenu2 = qobject_cast<QMenu*>(caused))
276  caused_menu = qmenu2->macMenu();
277  else if (QMenuBar *qmenubar2 = qobject_cast<QMenuBar*>(caused))
278  caused_menu = qmenubar2->macMenu();
279  else
280  caused_menu = 0;
281  }
282  }
283  return true;
284 }
uchar ignore_accel
Definition: qmenu_p.h:106
QPointer< QWidget > widget
bool showStatusText(QWidget *widget=0)
Updates the relevant status bar for the widget specified by sending a QStatusTipEvent to its parent w...
Definition: qaction.cpp:1315
void activate(ActionEvent event)
Sends the relevant signals for ActionEvent event.
Definition: qaction.cpp:1326
QMacMenuAction * action
Definition: qmenu_p.h:121
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
MenuCommand command
Definition: qmenu_p.h:116
const UInt32 kMenuCreatorQt
Definition: qmenu_mac.mm:82
static struct @304 qt_mac_current_menubar
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
QPointer< QMenuBar > qmenubar
Definition: qmenu_mac.mm:98
The QMenuBar class provides a horizontal menu bar.
Definition: qmenubar.h:62
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
struct OpaqueMenuRef * MenuRef
QPointer< QAction > action
Definition: qmenu_p.h:108
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void hovered(QAction *action)
This signal is emitted when a menu action is highlighted; action is the action that caused the event ...

◆ qt_mac_emit_menuSignals

void qt_mac_emit_menuSignals ( QMenu ,
bool   
)
friend

◆ qt_mac_menu_emit_hovered

void qt_mac_menu_emit_hovered ( QMenu menu,
QAction action 
)
friend

◆ qt_mac_menu_event

OSStatus qt_mac_menu_event ( EventHandlerCallRef  er,
EventRef  event,
void *   
)
friend

Definition at line 397 of file qmenu_mac.mm.

398 {
399  QScopedLoopLevelCounter loopLevelCounter(QApplicationPrivate::instance()->threadData);
400 
401  bool handled_event = true;
402  UInt32 ekind = GetEventKind(event), eclass = GetEventClass(event);
403  switch(eclass) {
404  case kEventClassCommand:
405  if (ekind == kEventCommandProcess) {
406  UInt32 context;
407  GetEventParameter(event, kEventParamMenuContext, typeUInt32,
408  0, sizeof(context), 0, &context);
409  HICommand cmd;
410  GetEventParameter(event, kEventParamDirectObject, typeHICommand,
411  0, sizeof(cmd), 0, &cmd);
412  if (!mac_keyboard_grabber && (context & kMenuContextKeyMatching)) {
413  QMacMenuAction *action = 0;
414  if (GetMenuCommandProperty(cmd.menu.menuRef, cmd.commandID, kMenuCreatorQt,
415  kMenuPropertyQAction, sizeof(action), 0, &action) == noErr) {
416  QWidget *widget = 0;
417  if (qApp->activePopupWidget())
418  widget = (qApp->activePopupWidget()->focusWidget() ?
419  qApp->activePopupWidget()->focusWidget() : qApp->activePopupWidget());
422  if (widget) {
423  int key = action->action->shortcut();
425  Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask));
426  accel_ev.ignore();
427  qt_sendSpontaneousEvent(widget, &accel_ev);
428  if (accel_ev.isAccepted()) {
429  handled_event = false;
430  break;
431  }
432  }
433  }
434  }
435  handled_event = qt_mac_activate_action(cmd.menu.menuRef, cmd.commandID,
436  QAction::Trigger, context & kMenuContextKeyMatching);
437  }
438  break;
439  case kEventClassMenu: {
440  MenuRef menu;
441  GetEventParameter(event, kEventParamDirectObject, typeMenuRef, NULL, sizeof(menu), NULL, &menu);
442  if (ekind == kEventMenuMatchKey) {
443  // Don't activate any actions if we are showing a native modal dialog,
444  // the key events should go to the dialog in this case.
446  return menuItemNotFoundErr;
447 
448  handled_event = false;
449  } else if (ekind == kEventMenuTargetItem) {
450  MenuCommand command;
451  GetEventParameter(event, kEventParamMenuCommand, typeMenuCommand,
452  0, sizeof(command), 0, &command);
453  handled_event = qt_mac_activate_action(menu, command, QAction::Hover, false);
454  } else if (ekind == kEventMenuOpening || ekind == kEventMenuClosed) {
455  qt_mac_menus_open_count += (ekind == kEventMenuOpening) ? 1 : -1;
456  MenuRef mr;
457  GetEventParameter(event, kEventParamDirectObject, typeMenuRef,
458  0, sizeof(mr), 0, &mr);
459 
460  QWidget *widget = 0;
461  if (GetMenuItemProperty(mr, 0, kMenuCreatorQt, kMenuPropertyQWidget, sizeof(widget), 0, &widget) == noErr) {
462  if (QMenu *qmenu = qobject_cast<QMenu*>(widget)) {
463  handled_event = true;
464  if (ekind == kEventMenuOpening) {
465  emit qmenu->aboutToShow();
466 
467  int merged = 0;
468  const QMenuPrivate::QMacMenuPrivate *mac_menu = qmenu->d_func()->mac_menu;
469  const int ActionItemsCount = mac_menu->actionItems.size();
470  for(int i = 0; i < ActionItemsCount; ++i) {
471  QMacMenuAction *action = mac_menu->actionItems.at(i);
472  if (action->action->isSeparator()) {
473  bool hide = false;
474  if(!action->action->isVisible()) {
475  hide = true;
476  } else if (merged && merged == i) {
477  hide = true;
478  } else {
479  for(int l = i+1; l < mac_menu->actionItems.size(); ++l) {
480  QMacMenuAction *action = mac_menu->actionItems.at(l);
481  if (action->merged) {
482  hide = true;
483  } else if (action->action->isSeparator()) {
484  if (hide)
485  break;
486  } else if (!action->merged) {
487  hide = false;
488  break;
489  }
490  }
491  }
492 
493  const int index = qt_mac_menu_find_action(mr, action);
494  if (hide) {
495  ++merged;
496  ChangeMenuItemAttributes(mr, index, kMenuItemAttrHidden, 0);
497  } else {
498  ChangeMenuItemAttributes(mr, index, 0, kMenuItemAttrHidden);
499  }
500  } else if (action->merged) {
501  ++merged;
502  }
503  }
504  } else {
505  emit qmenu->aboutToHide();
506  }
507  }
508  }
509  } else {
510  handled_event = false;
511  }
512  break; }
513  default:
514  handled_event = false;
515  break;
516  }
517  if (!handled_event) //let the event go through
518  return CallNextEventHandler(er, event);
519  return noErr; //we eat the event
520 }
bool isSeparator() const
Returns true if this action is a separator action; otherwise it returns false.
Definition: qaction.cpp:839
The QKeyEvent class describes a key event.
Definition: qevent.h:224
QPointer< QWidget > widget
QKeySequence shortcut
the action&#39;s primary shortcut key
Definition: qaction.h:83
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QList< QMacMenuAction * > actionItems
Definition: qmenu_p.h:308
static QApplicationPrivate * instance()
friend bool qt_mac_activate_action(OSMenuRef, uint, QAction::ActionEvent, bool)
Definition: qmenu_mac.mm:209
static QWidget * focus_widget
bool isVisible() const
Definition: qaction.cpp:1246
const UInt32 kMenuCreatorQt
Definition: qmenu_mac.mm:82
#define qApp
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
void hide()
Hides the widget.
Definition: qwidget.h:501
bool event(QEvent *)
Reimplemented Function
Definition: qmenu.cpp:2447
static int qt_mac_menu_find_action(MenuRef menu, MenuCommand cmd)
Definition: qmenu_mac.mm:287
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
int key
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static bool native_modal_dialog_active
QFactoryLoader * l
struct OpaqueMenuRef * MenuRef
quint16 index
QWidget * mac_keyboard_grabber
Definition: qwidget_mac.mm:157
QPointer< QAction > action
Definition: qmenu_p.h:108
bool qt_sendSpontaneousEvent(QObject *, QEvent *)
uchar merged
Definition: qmenu_p.h:107
int qt_mac_menus_open_count
Definition: qmenu_mac.mm:76

◆ qt_mac_trayicon_activate_action

void qt_mac_trayicon_activate_action ( QMenu ,
QAction action 
)
friend

◆ qt_mac_watchingAboutToShow

bool qt_mac_watchingAboutToShow ( QMenu menu)
friend

Definition at line 119 of file qmenu_mac.mm.

120 {
121  return menu && menu->receivers(SIGNAL(aboutToShow()));
122 }
#define SIGNAL(a)
Definition: qobjectdefs.h:227
int receivers(const char *signal) const
Returns the number of receivers connected to the signal.
Definition: qobject.cpp:2406
void aboutToShow()
This signal is emitted just before the menu is shown to the user.

◆ QToolButtonPrivate

friend class QToolButtonPrivate
friend

Definition at line 416 of file qmenu.h.

◆ QTornOffMenu

friend class QTornOffMenu
friend

Definition at line 412 of file qmenu.h.

Properties

◆ icon

QIcon QMenu::icon
private

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.

Definition at line 80 of file qmenu.h.

◆ separatorsCollapsible

bool QMenu::separatorsCollapsible
private

whether consecutive separators should be collapsed

Since
4.2

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.

Definition at line 81 of file qmenu.h.

◆ tearOffEnabled

QMenu::tearOffEnabled
private

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.

Definition at line 78 of file qmenu.h.

◆ title

QString QMenu::title
private

The title of the menu.

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

By default, this property contains an empty string.

Definition at line 79 of file qmenu.h.

Referenced by QAccessibleMenuItem::text().


The documentation for this class was generated from the following files: