Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QMenu Class

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

Header:#include <QMenu>
Inherits:QWidget
Inherited By:

Q3PopupMenu

Properties

Public Functions

QMenu(QWidget * parent = 0)
QMenu(const QString & title, QWidget * parent = 0)
~QMenu()
QAction *actionAt(const QPoint & pt) const
QRectactionGeometry(QAction * act) const
QAction *activeAction() const
QAction *addAction(const QString & text)
QAction *addAction(const QIcon & icon, const QString & text)
QAction *addAction(const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0)
QAction *addAction(const QIcon & icon, const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0)
voidaddAction(QAction * action)
QAction *addMenu(QMenu * menu)
QMenu *addMenu(const QString & title)
QMenu *addMenu(const QIcon & icon, const QString & title)
QAction *addSeparator()
voidclear()
QAction *defaultAction() const
QAction *exec()
QAction *exec(const QPoint & p, QAction * action = 0)
voidhideTearOffMenu()
QIconicon() const
QAction *insertMenu(QAction * before, QMenu * menu)
QAction *insertSeparator(QAction * before)
boolisEmpty() const
boolisTearOffEnabled() const
boolisTearOffMenuVisible() const
QAction *menuAction() const
voidpopup(const QPoint & p, QAction * atAction = 0)
boolseparatorsCollapsible() const
voidsetActiveAction(QAction * act)
voidsetDefaultAction(QAction * act)
voidsetIcon(const QIcon & icon)
voidsetSeparatorsCollapsible(bool collapse)
voidsetTearOffEnabled(bool)
voidsetTitle(const QString & title)
QStringtitle() const

Reimplemented Public Functions

virtual QSizesizeHint() const

Signals

voidaboutToHide()
voidaboutToShow()
voidhovered(QAction * action)
voidtriggered(QAction * action)

Static Public Members

QAction *exec(QList<QAction *> actions, const QPoint & pos, QAction * at, QWidget * parent)
QAction *exec(QList<QAction *> actions, const QPoint & pos, QAction * at = 0)
  • 4 static public members inherited fromQWidget
  • 7 static public members inherited fromQObject

Protected Functions

intcolumnCount() const
voidinitStyleOption(QStyleOptionMenuItem * option, const QAction * action) const

Reimplemented Protected Functions

virtual voidactionEvent(QActionEvent * e)
virtual voidchangeEvent(QEvent * e)
virtual voidenterEvent(QEvent *)
virtual boolevent(QEvent * e)
virtual boolfocusNextPrevChild(bool next)
virtual voidhideEvent(QHideEvent *)
virtual voidkeyPressEvent(QKeyEvent * e)
virtual voidleaveEvent(QEvent *)
virtual voidmouseMoveEvent(QMouseEvent * e)
virtual voidmousePressEvent(QMouseEvent * e)
virtual voidmouseReleaseEvent(QMouseEvent * e)
virtual voidpaintEvent(QPaintEvent * e)
virtual voidtimerEvent(QTimerEvent * e)
virtual voidwheelEvent(QWheelEvent * e)
  • 37 protected functions inherited fromQWidget
  • 8 protected functions inherited fromQObject
  • 1 protected function inherited fromQPaintDevice

Additional Inherited Members

  • 19 public slots inherited fromQWidget
  • 1 public slot inherited fromQObject
  • 1 protected slot inherited fromQWidget

Detailed Description

TheQMenu 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. UseQMenuBar::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 withpopup() or synchronously withexec(). Menus can also be invoked in response to button presses; these are just like context menus except for how they are invoked.

Fig. A menu shown inPlastique widget style,Windows XP widget style, andMacintosh widget style.

Actions

A menu consists of a list of action items. Actions are added with theaddAction(),addActions() andinsertAction() functions. An action is represented vertically and rendered byQStyle. 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 withactions().

There are four kinds of action items: separators, actions that show a submenu, widgets, and actions that perform an action. Separators are inserted withaddSeparator(), submenus withaddMenu(), 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 istriggered(). In addition,QMenu provides two signals,activated() andhighlighted(), which signal theQAction that was triggered from the menu.

You clear a menu withclear() and remove individual action items withremoveAction().

AQMenu 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 withsetTearOffEnabled(). 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 aQToolBar instead.

Widgets can be inserted into menus with theQWidgetAction class. Instances of this class are used to hold widgets, and are inserted into menus with theaddAction() overload that takes aQAction.

Conversely, actions can be added to widgets with theaddAction(),addActions() andinsertAction() functions.

Warning: To makeQMenu visible on the screen,exec() orpopup() should be used instead ofshow().

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 theMenus example for an example of how to useQMenuBar andQMenu in your application.

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

See alsoQMenuBar,GUI Design Handbook: Menu, Drop-Down and Pop-Up,Application Example,Menus Example, andRecent Files Example.

Property Documentation

icon :QIcon

This property holds the icon of the menu.

This is equivalent to theQAction::icon property of themenuAction().

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

Access functions:

QIconicon() const
voidsetIcon(const QIcon & icon)

separatorsCollapsible :bool

This property holds whether consecutive separators should be collapsed.

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

By default, this property is true.

This property was introduced in Qt 4.2.

Access functions:

boolseparatorsCollapsible() const
voidsetSeparatorsCollapsible(bool collapse)

tearOffEnabled :bool

This property holds whether the menu supports being torn off.

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

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

By default, this property is false.

Access functions:

boolisTearOffEnabled() const
voidsetTearOffEnabled(bool)

title :QString

This property holds the title of the menu.

This is equivalent to theQAction::text property of themenuAction().

By default, this property contains an empty string.

Access functions:

QStringtitle() const
voidsetTitle(const QString & title)

Member Function Documentation

QMenu::QMenu(QWidget * parent = 0)

Constructs a menu with parentparent.

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

QMenu::QMenu(constQString & title,QWidget * parent = 0)

Constructs a menu with atitle and aparent.

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

See alsotitle.

QMenu::~QMenu()

Destroys the menu.

[signal]void QMenu::aboutToHide()

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

This function was introduced in Qt 4.2.

See alsoaboutToShow() andhide().

[signal]void QMenu::aboutToShow()

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

See alsoaboutToHide() andshow().

QAction * QMenu::actionAt(constQPoint & pt) const

Returns the item atpt; returns 0 if there is no item there.

[virtual protected]void QMenu::actionEvent(QActionEvent * e)

Reimplemented fromQWidget::actionEvent().

QRect QMenu::actionGeometry(QAction * act) const

Returns the geometry of actionact.

QAction * QMenu::activeAction() const

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

See alsosetActiveAction().

QAction * QMenu::addAction(constQString & text)

This is an overloaded function.

This convenience function creates a new action withtext. The function adds the newly created action to the menu's list of actions, and returns it.

See alsoQWidget::addAction().

QAction * QMenu::addAction(constQIcon & icon, constQString & text)

This is an overloaded function.

This convenience function creates a new action with anicon and sometext. The function adds the newly created action to the menu's list of actions, and returns it.

See alsoQWidget::addAction().

QAction * QMenu::addAction(constQString & text, constQObject * receiver, constchar * member, constQKeySequence & shortcut = 0)

This is an overloaded function.

This convenience function creates a new action with the texttext and an optional shortcutshortcut. The action'striggered() signal is connected to thereceiver'smember slot. The function adds the newly created action to the menu's list of actions and returns it.

See alsoQWidget::addAction().

QAction * QMenu::addAction(constQIcon & icon, constQString & text, constQObject * receiver, constchar * member, constQKeySequence & shortcut = 0)

This is an overloaded function.

This convenience function creates a new action with anicon and sometext and an optional shortcutshortcut. The action'striggered() signal is connected to themember slot of thereceiver object. The function adds the newly created action to the menu's list of actions, and returns it.

See alsoQWidget::addAction().

void QMenu::addAction(QAction * action)

This is an overloaded function.

Appends the actionaction to the menu's list of actions.

See alsoQMenuBar::addAction() andQWidget::addAction().

QAction * QMenu::addMenu(QMenu * menu)

This convenience function addsmenu as a submenu to this menu. It returnsmenu'smenuAction(). This menu does not take ownership ofmenu.

See alsoQWidget::addAction() andQMenu::menuAction().

QMenu * QMenu::addMenu(constQString & title)

Appends a newQMenu withtitle to the menu. The menu takes ownership of the menu. Returns the new menu.

See alsoQWidget::addAction() andQMenu::menuAction().

QMenu * QMenu::addMenu(constQIcon & icon, constQString & title)

Appends a newQMenu withicon andtitle to the menu. The menu takes ownership of the menu. Returns the new menu.

See alsoQWidget::addAction() andQMenu::menuAction().

QAction * QMenu::addSeparator()

This convenience function creates a new separator action, i.e. an action withQAction::isSeparator() returning true, and adds the new action to this menu's list of actions. It returns the newly created action.

See alsoQWidget::addAction().

[virtual protected]void QMenu::changeEvent(QEvent * e)

Reimplemented fromQWidget::changeEvent().

void QMenu::clear()

Removes all the menu's actions. Actions owned by the menu and not shown in any other widget are deleted.

See alsoremoveAction().

[protected]int QMenu::columnCount() const

If a menu does not fit on the screen it lays itself out so that it does fit. It is style dependent what layout means (for example, on Windows it will use multiple columns).

This functions returns the number of columns necessary.

QAction * QMenu::defaultAction() const

Returns the current default action.

See alsosetDefaultAction().

[virtual protected]void QMenu::enterEvent(QEvent *)

Reimplemented fromQWidget::enterEvent().

[virtual protected]bool QMenu::event(QEvent * e)

Reimplemented fromQObject::event().

QAction * QMenu::exec()

Executes this menu synchronously.

This is equivalent toexec(pos()).

This returns the triggeredQAction 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:

exec(QCursor::pos());

or aligned to a widget:

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

or in reaction to aQMouseEvent *e:

exec(e->globalPos());

QAction * QMenu::exec(constQPoint & p,QAction * action = 0)

This is an overloaded function.

Executes this menu synchronously.

Pops up the menu so that the actionaction will be at the specifiedglobal positionp. To translate a widget's local coordinates into global coordinates, useQWidget::mapToGlobal().

This returns the triggeredQAction 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 aQAction to a slot and call the menu'sexec(), you get the result both via the signal-slot connection and in the return value ofexec().

Common usage is to position the menu at the current mouse position:

exec(QCursor::pos());

or aligned to a widget:

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

or in reaction to aQMouseEvent *e:

exec(e->globalPos());

When positioning a menu withexec() orpopup(), bear in mind that you cannot rely on the menu's currentsize(). 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, usesizeHint() which calculates the proper size depending on the menu's current contents.

See alsopopup() andQWidget::mapToGlobal().

[static]QAction * QMenu::exec(QList<QAction *> actions, constQPoint & pos,QAction * at,QWidget * parent)

This is an overloaded function.

Executes a menu synchronously.

The menu's actions are specified by the list ofactions. The menu will pop up so that the specified action,at, appears at global positionpos. Ifat is not specified then the menu appears at positionpos.parent is the menu's parent widget; specifying the parent will provide context whenpos alone is not enough to decide where the menu should go (e.g., with multiple desktops or when the parent is embedded inQGraphicsView).

The function returns the triggeredQAction 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 emptyforeach (QAction*a, actions)    menu.addAction(a);menu.exec(pos, at);

See alsopopup() andQWidget::mapToGlobal().

[static]QAction * QMenu::exec(QList<QAction *> actions, constQPoint & pos,QAction * at = 0)

This is an overloaded function.

Executes a menu synchronously.

The menu's actions are specified by the list ofactions. The menu will pop up so that the specified action,at, appears at global positionpos. Ifat is not specified then the menu appears at positionpos.

The function returns the triggeredQAction 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 emptyforeach (QAction*a, actions)    menu.addAction(a);menu.exec(pos, at);

See alsopopup() andQWidget::mapToGlobal().

[virtual protected]bool QMenu::focusNextPrevChild(bool next)

Reimplemented fromQWidget::focusNextPrevChild().

[virtual protected]void QMenu::hideEvent(QHideEvent *)

Reimplemented fromQWidget::hideEvent().

void QMenu::hideTearOffMenu()

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

See alsoisTearOffMenuVisible() andisTearOffEnabled().

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

This signal is emitted when a menu action is highlighted;action is the action that caused the signal to be emitted.

Often this is used to update status information.

See alsotriggered() andQAction::hovered().

[protected]void QMenu::initStyleOption(QStyleOptionMenuItem * option, constQAction * action) const

Initializeoption with the values from this menu and information fromaction. This method is useful for subclasses when they need aQStyleOptionMenuItem, but don't want to fill in all the information themselves.

See alsoQStyleOption::initFrom() andQMenuBar::initStyleOption().

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

This convenience function insertsmenu before actionbefore and returns the menusmenuAction().

See alsoQWidget::insertAction() andaddMenu().

QAction * QMenu::insertSeparator(QAction * before)

This convenience function creates a new separator action, i.e. an action withQAction::isSeparator() returning true. The function inserts the newly created action into this menu's list of actions before actionbefore and returns it.

See alsoQWidget::insertAction() andaddSeparator().

bool QMenu::isEmpty() const

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

This function was introduced in Qt 4.2.

See alsoQWidget::actions().

bool QMenu::isTearOffMenuVisible() const

When a menu is torn off a second menu is shown to display the menu contents in a new window. When the menu is in this mode and the menu is visible returns true; otherwise false.

See alsohideTearOffMenu() andisTearOffEnabled().

[virtual protected]void QMenu::keyPressEvent(QKeyEvent * e)

Reimplemented fromQWidget::keyPressEvent().

[virtual protected]void QMenu::leaveEvent(QEvent *)

Reimplemented fromQWidget::leaveEvent().

QAction * QMenu::menuAction() const

Returns the action associated with this menu.

[virtual protected]void QMenu::mouseMoveEvent(QMouseEvent * e)

Reimplemented fromQWidget::mouseMoveEvent().

[virtual protected]void QMenu::mousePressEvent(QMouseEvent * e)

Reimplemented fromQWidget::mousePressEvent().

[virtual protected]void QMenu::mouseReleaseEvent(QMouseEvent * e)

Reimplemented fromQWidget::mouseReleaseEvent().

[virtual protected]void QMenu::paintEvent(QPaintEvent * e)

Reimplemented fromQWidget::paintEvent().

void QMenu::popup(constQPoint & p,QAction * atAction = 0)

Displays the menu so that the actionatAction will be at the specifiedglobal positionp. To translate a widget's local coordinates into global coordinates, useQWidget::mapToGlobal().

When positioning a menu withexec() or popup(), bear in mind that you cannot rely on the menu's currentsize(). 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, usesizeHint() which calculates the proper size depending on the menu's current contents.

See alsoQWidget::mapToGlobal() andexec().

void QMenu::setActiveAction(QAction * act)

Sets the currently highlighted action toact.

See alsoactiveAction().

void QMenu::setDefaultAction(QAction * act)

This sets the default action toact. The default action may have a visual cue, depending on the currentQStyle. A default action usually indicates what will happen by default when a drop occurs.

See alsodefaultAction().

[virtual]QSize QMenu::sizeHint() const

Reimplemented fromQWidget::sizeHint().

[virtual protected]void QMenu::timerEvent(QTimerEvent * e)

Reimplemented fromQObject::timerEvent().

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

This signal is emitted when an action in this menu is triggered.

action is the action that caused the signal to be emitted.

Normally, you connect each menu action'striggered() 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 alsohovered() andQAction::triggered().

[virtual protected]void QMenu::wheelEvent(QWheelEvent * e)

Reimplemented fromQWidget::wheelEvent().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp