QtBase
v6.3.1
|
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus. More...
#include <qmenu.h>
Signals | |
void | aboutToShow () |
void | aboutToHide () |
void | triggered (QAction *action) |
void | hovered (QAction *action) |
Signals inherited from QWidget | |
void | windowTitleChanged (const QString &title) |
void | windowIconChanged (const QIcon &icon) |
void | windowIconTextChanged (const QString &iconText) |
void | customContextMenuRequested (const QPoint &pos) |
Signals inherited from QObject | |
void | destroyed (QObject *=nullptr) |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
Properties | |
bool | tearOffEnabled |
whether the menu supports being torn off More... | |
QString | title |
The title of the menu. More... | |
QIcon | icon |
The icon of the menu. More... | |
bool | separatorsCollapsible |
whether consecutive separators should be collapsed More... | |
bool | toolTipsVisible |
whether tooltips of menu actions should be visible More... | |
Properties inherited from QWidget | |
bool | modal |
whether the widget is a modal widget More... | |
Qt::WindowModality | windowModality |
which windows are blocked by the modal widget More... | |
bool | enabled |
whether the widget is enabled More... | |
QRect | geometry |
the geometry of the widget relative to its parent and excluding the window frame More... | |
QRect | frameGeometry |
geometry of the widget relative to its parent including any window frame More... | |
QRect | normalGeometry |
the geometry of the widget as it will appear when shown as a normal (not maximized or full screen) top-level widget More... | |
int | x |
the x coordinate of the widget relative to its parent including any window frame More... | |
int | y |
the y coordinate of the widget relative to its parent and including any window frame More... | |
QPoint | pos |
the position of the widget within its parent widget More... | |
QSize | frameSize |
the size of the widget including any window frame More... | |
QSize | size |
the size of the widget excluding any window frame More... | |
int | width |
the width of the widget excluding any window frame More... | |
int | height |
the height of the widget excluding any window frame More... | |
QRect | rect |
the internal geometry of the widget excluding any window frame More... | |
QRect | childrenRect |
the bounding rectangle of the widget's children More... | |
QRegion | childrenRegion |
the combined region occupied by the widget's children More... | |
QSizePolicy | sizePolicy |
the default layout behavior of the widget More... | |
QSize | minimumSize |
the widget's minimum size More... | |
QSize | maximumSize |
the widget's maximum size in pixels More... | |
int | minimumWidth |
the widget's minimum width in pixels More... | |
int | minimumHeight |
the widget's minimum height in pixels More... | |
int | maximumWidth |
the widget's maximum width in pixels More... | |
int | maximumHeight |
the widget's maximum height in pixels More... | |
QSize | sizeIncrement |
the size increment of the widget More... | |
QSize | baseSize |
the base size of the widget More... | |
QPalette | palette |
the widget's palette More... | |
QFont | font |
the font currently set for the widget More... | |
QCursor | cursor |
the cursor shape for this widget More... | |
bool | mouseTracking |
whether mouse tracking is enabled for the widget More... | |
bool | tabletTracking |
whether tablet tracking is enabled for the widget More... | |
bool | isActiveWindow |
whether this widget's window is the active window More... | |
Qt::FocusPolicy | focusPolicy |
the way the widget accepts keyboard focus More... | |
bool | focus |
whether this widget (or its focus proxy) has the keyboard input focus More... | |
Qt::ContextMenuPolicy | contextMenuPolicy |
how the widget shows a context menu More... | |
bool | updatesEnabled |
whether updates are enabled More... | |
bool | visible |
whether the widget is visible More... | |
bool | minimized |
whether this widget is minimized (iconified) More... | |
bool | maximized |
whether this widget is maximized More... | |
bool | fullScreen |
whether the widget is shown in full screen mode More... | |
QSize | sizeHint |
the recommended size for the widget More... | |
QSize | minimumSizeHint |
the recommended minimum size for the widget More... | |
bool | acceptDrops |
whether drop events are enabled for this widget More... | |
QString | windowTitle |
the window title (caption) More... | |
QIcon | windowIcon |
the widget's icon More... | |
QString | windowIconText |
the text to be displayed on the icon of a minimized window More... | |
double | windowOpacity |
The level of opacity for the window. More... | |
bool | windowModified |
whether the document shown in the window has unsaved changes More... | |
QString | accessibleName |
the widget's name as seen by assistive technologies More... | |
QString | accessibleDescription |
the widget's description as seen by assistive technologies More... | |
Qt::LayoutDirection | layoutDirection |
the layout direction for this widget. More... | |
bool | autoFillBackground |
whether the widget background is filled automatically More... | |
QString | styleSheet |
the widget's style sheet More... | |
QLocale | locale |
the widget's locale More... | |
QString | windowFilePath |
the file path associated with a widget More... | |
Qt::InputMethodHints | inputMethodHints |
What input method specific hints the widget has. More... | |
Properties inherited from QObject | |
QString | objectName |
the name of this object More... | |
Friends | |
class | QMenuBar |
class | QMenuBarPrivate |
class | QTornOffMenu |
class | QComboBox |
class | QtWidgetsActionPrivate |
class | QToolButtonPrivate |
void | qt_mac_emit_menuSignals (QMenu *menu, bool show) |
void | qt_mac_menu_emit_hovered (QMenu *menu, QAction *action) |
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
\inmodule QtWidgets
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.
QMenu::~QMenu | ( | ) |
|
signal |
This signal is emitted just before the menu is hidden from the user.
|
signal |
This signal is emitted just before the menu is shown to the user.
|
overrideprotectedvirtual |
QAction * QMenu::activeAction | ( | ) | const |
QAction * QWidget::addAction |
Definition at line 598 of file qwidget.cpp.
|
inline |
QAction* QWidget::addAction |
QAction * QWidget::addAction |
Definition at line 597 of file qwidget.cpp.
|
inline |
QAction* QWidget::addAction |
void QWidget::addAction |
Appends the action action to this widget's list of actions.
All QWidgets have a list of \l{QAction}s, however they can be represented graphically in many different ways. The default use of the QAction list (as returned by actions()) is to create a context QMenu.
A QWidget should only have one of each action and adding an action it already has will not cause the same action to be in the widget twice.
The ownership of action is not transferred to this QWidget.
Definition at line 589 of file qwidget.cpp.
Appends a new QMenu with icon and title to the menu. The menu takes ownership of the menu. Returns the new menu.
Definition at line 1897 of file qmenu.cpp.
Appends a new QMenu with title to the menu. The menu takes ownership of the menu. Returns the new menu.
Definition at line 1884 of file qmenu.cpp.
This convenience function adds menu as a submenu to this menu. It returns menu's menuAction(). This menu does not take ownership of menu.
Definition at line 1871 of file qmenu.cpp.
This convenience function creates a new section action, i.e. an action with QAction::isSeparator() returning true but also having text and icon hints, and adds the new action to this menu's list of actions. It returns the newly created action.
The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.
QMenu takes ownership of the returned QAction.
Definition at line 1963 of file qmenu.cpp.
This convenience function creates a new section action, i.e. an action with QAction::isSeparator() returning true but also having text hint, and adds the new action to this menu's list of actions. It returns the newly created action.
The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
QMenu takes ownership of the returned QAction.
Definition at line 1939 of file qmenu.cpp.
QAction * QMenu::addSeparator | ( | ) |
This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true, and adds the new action to this menu's list of actions. It returns the newly created action.
QMenu takes ownership of the returned QAction.
Definition at line 1915 of file qmenu.cpp.
void QMenu::clear | ( | ) |
Removes all the menu's actions. Actions owned by the menu and not shown in any other widget are deleted.
Definition at line 2219 of file qmenu.cpp.
|
protected |
QAction * QMenu::defaultAction | ( | ) | const |
Returns the current default action.
|
overrideprotectedvirtual |
|
overrideprotectedvirtual |
QAction * QMenu::exec | ( | ) |
Executes this menu synchronously.
This is equivalent to {exec(pos())}.
This returns the triggered QAction in either the popup menu or one of its submenus, or \nullptr if no item was triggered (normally because the user pressed Esc).
In most situations you'll want to specify the position yourself, for example, the current mouse position:
or aligned to a widget:
or in reaction to a QMouseEvent *e:
Definition at line 2602 of file qmenu.cpp.
|
static |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Executes a menu synchronously.
The menu's actions are specified by the list of actions. The menu will pop up so that the specified action, at, appears at global position pos. If at is not specified then the menu appears at position pos. parent is the menu's parent widget; specifying the parent will provide context when pos alone is not enough to decide where the menu should go (e.g., with multiple desktops or when the parent is embedded in QGraphicsView).
The function returns the triggered QAction in either the popup menu or one of its submenus, or \nullptr if no item was triggered (normally because the user pressed Esc).
This is equivalent to:
Definition at line 2691 of file qmenu.cpp.
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 \nullptr if no item was triggered (normally because the user pressed Esc).
Note that all signals are emitted as usual. If you connect a QAction to a slot and call the menu's exec(), you get the result both via the signal-slot connection and in the return value of exec().
Common usage is to position the menu at the current mouse position:
or aligned to a widget:
or in reaction to a QMouseEvent *e:
When positioning a menu with exec() or popup(), bear in mind that you cannot rely on the menu's current size(). For performance reasons, the menu adapts its size only when necessary. So in many cases, the size before and after the show is different. Instead, use sizeHint() which calculates the proper size depending on the menu's current contents.
|
overrideprotectedvirtual |
|
overrideprotectedvirtual |
void QMenu::hideTearOffMenu | ( | ) |
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.
|
protectedvirtual |
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.
Definition at line 1593 of file qmenu.cpp.
This convenience function inserts menu before action before and returns the menus menuAction().
Definition at line 1977 of file qmenu.cpp.
This convenience function creates a new title action, i.e. an action with QAction::isSeparator() returning true but also having text and icon hints. The function inserts the newly created action into this menu's list of actions before action before and returns it.
The rendering of the hints is style and platform dependent. Widget styles can use the text and icon information in the rendering for sections, or can choose to ignore them and render sections like simple separators.
QMenu takes ownership of the returned QAction.
Definition at line 2043 of file qmenu.cpp.
This convenience function creates a new title action, i.e. an action with QAction::isSeparator() returning true but also having text hint. The function inserts the newly created action into this menu's list of actions before action before and returns it.
The rendering of the hint is style and platform dependent. Widget styles can use the text information in the rendering for sections, or can choose to ignore it and render sections like simple separators.
QMenu takes ownership of the returned QAction.
Definition at line 2019 of file qmenu.cpp.
This convenience function creates a new separator action, i.e. an action with QAction::isSeparator() returning true. The function inserts the newly created action into this menu's list of actions before action before and returns it.
QMenu takes ownership of the returned QAction.
Definition at line 1994 of file qmenu.cpp.
bool QMenu::isEmpty | ( | ) | const |
Returns true
if there are no visible actions inserted into the menu, false otherwise.
Definition at line 2201 of file qmenu.cpp.
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.
QAction * QMenu::menuAction | ( | ) | const |
|
overrideprotectedvirtual |
|
overrideprotectedvirtual |
|
overrideprotectedvirtual |
|
overrideprotectedvirtual |
QPlatformMenu * QMenu::platformMenu | ( | ) |
Displays the menu so that the action atAction will be at the specified global position p. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().
When positioning a menu with exec() or popup(), bear in mind that you cannot rely on the menu's current size(). For performance reasons, the menu adapts its size only when necessary, so in many cases, the size before and after the show is different. Instead, use sizeHint() which calculates the proper size depending on the menu's current contents.
Definition at line 2304 of file qmenu.cpp.
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.
void QMenu::setPlatformMenu | ( | QPlatformMenu * | platformMenu | ) |
void QMenu::setSeparatorsCollapsible | ( | bool | collapse | ) |
void QMenu::setTearOffEnabled | ( | bool | b | ) |
void QMenu::showTearOffMenu | ( | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function will forcibly show the torn off menu making it appear on the user's desktop under the mouse currsor.
Definition at line 2146 of file qmenu.cpp.
|
overridevirtual |
|
overrideprotectedvirtual |
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 \l{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".
|
friend |
|
friend |
|
friend |
|
friend |
|
readwrite |
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.
|
readwrite |
whether consecutive separators should be collapsed
This property specifies whether consecutive separators in the menu should be visually collapsed to a single one. Separators at the beginning or the end of the menu are also hidden.
By default, this property is true
.
|
readwrite |
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
.
|
readwrite |
The title of the menu.
This is equivalent to the QAction::text property of the menuAction().
By default, this property contains an empty string.
|
readwrite |