Home · All Classes · Main Classes · Grouped Classes · Modules · Functions

QMenu Class Reference
[QtGui module]

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

 #include <QMenu>

Inherits QWidget.

Inherited by Q3PopupMenu.

Properties

Public Functions

Signals

Static Public Members

Protected Functions

Additional Inherited Members


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.

A menu consists of a list of action items. Actions are added with addAction(). 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".

There are three kinds of action items: separators, actions that show a submenu, and actions that perform an action. Separators are inserted with addSeparator(). For submenus use addMenu(). 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.

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, GUI Design Handbook: Menu, Drop-Down and Pop-Up, Application Example, Menus Example, and Recent Files Example.


Property Documentation

icon : QIcon

This property holds the icon of the menu.

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

Access functions:

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.

This property was introduced in Qt 4.2.

Access functions:

tearOffEnabled : bool

This property holds whether the menu supports being torn off.

When true, QMenu has a special menu item (often shown as a dashed line at the top of the menu) that creates a copy of the menu when the tear-off menu item 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.

Access functions:

title : QString

This property holds the title of the menu.

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

Access functions:


Member Function Documentation

QMenu::QMenu ( QWidget * parent = 0 )

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

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

Constructs a menu with a title and a parent.

Although a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed (as with any other QObject).

See also title.

QMenu::~QMenu ()

Destroys the menu.

void QMenu::aboutToHide ()   [signal]

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

This function was introduced in Qt 4.2.

See also aboutToShow() and hide().

void QMenu::aboutToShow ()   [signal]

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

See also aboutToHide() and show().

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

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

QRect QMenu::actionGeometry ( QAction * act ) const

Returns the geometry of action act.

QAction * QMenu::activeAction () const

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

See also setActiveAction().

QAction * QMenu::addAction ( const QString & text )

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

See also QWidget::addAction().

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

This is an overloaded member function, provided for convenience.

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

See also QWidget::addAction().

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

This is an overloaded member function, provided for convenience.

This convenience function creates a new action with the text text and an optional shortcut shortcut. The action's triggered() signal is connected to the receiver's member slot. The function adds the newly created action to the menu's list of actions and returns it.

See also QWidget::addAction().

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

This is an overloaded member function, provided for convenience.

This convenience function creates a new action with an icon and some text and an optional shortcut shortcut. The action's triggered() signal is connected to the member slot of the receiver object. The function adds the newly created action to the menu's list of actions, and returns it.

See also QWidget::addAction().

QAction * QMenu::addMenu ( QMenu * menu )

This convenience function adds menu as a submenu to this menu. It returns the menus menuAction().

See also QWidget::addAction() and QMenu::menuAction().

QMenu * QMenu::addMenu ( const QString & title )

This is an overloaded member function, provided for convenience.

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

See also QWidget::addAction() and QMenu::menuAction().

QMenu * QMenu::addMenu ( const QIcon & icon, const QString & title )

This is an overloaded member function, provided for convenience.

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

See also QWidget::addAction() and QMenu::menuAction().

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

void QMenu::clear ()

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

See also removeAction().

int QMenu::columnCount () const   [protected]

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

This functions returns the number of columns necessary.

QAction * QMenu::defaultAction () const

Returns the current default action.

See also setDefaultAction().

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:

 exec(QCursor::pos());

or aligned to a widget:

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

or in reaction to a QMouseEvent *e:

 exec(e->globalPos());

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

This is an overloaded member function, provided for convenience.

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:

 exec(QCursor::pos());

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

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

This is an overloaded member function, provided for convenience.

Executes this 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.

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

void QMenu::hideTearOffMenu ()

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

See also isTearOffMenuVisible() and isTearOffEnabled().

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

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

Often this is used to update status information.

See also triggered() and QAction::hovered().

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

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

See also QWidget::insertAction() and addMenu().

QAction * QMenu::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() and addSeparator().

bool QMenu::isEmpty () const

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

This function was introduced in Qt 4.2.

See also QWidget::actions().

bool QMenu::isTearOffMenuVisible () const

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

See also hideTearOffMenu() and isTearOffEnabled().

QAction * QMenu::menuAction () const

Returns the action associated with this menu.

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. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().

When positioning a menu with exec() or popup(), bear in mind that you cannot rely on the menu's current size(). For performance reasons, the menu adapts its size only when necessary, so in many cases, the size before and after the show is different. Instead, use sizeHint() which calculates the proper size depending on the menu's current contents.

See also QWidget::mapToGlobal() and exec().

void QMenu::setActiveAction ( QAction * act )

Sets the currently highlighted action to act.

See also activeAction().

void QMenu::setDefaultAction ( QAction * act )

This will set the default action to act. The default action may have a visual queue depending on the current QStyle. A default action is usually meant to indicate what will defaultly happen on a drop, as shown in a context menu.

See also defaultAction().

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

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

Normally, you connect each menu action's triggered() signal to its own custom slot, but sometimes you will want to connect several actions to a single slot, for example, when you have a group of closely related actions, such as "left justify", "center", "right justify".

See also hovered() and QAction::triggered().


Member Function Documentation

QKeySequence QMenu::accel ( int id ) const

Use shortcut() on the relevant QAction instead.

See also setAccel().

void QMenu::activateItemAt ( int index )

Use activate() on the relevant QAction instead.

void QMenu::activated ( int itemId )   [signal]

Use triggered() instead.

void QMenu::changeItem ( int id, const QString & text )

Use setText() on the relevant QAction instead.

void QMenu::changeItem ( int id, const QPixmap & pixmap )

This is an overloaded member function, provided for convenience.

Use setText() on the relevant QAction instead.

void QMenu::changeItem ( int id, const QIcon & icon, const QString & text )

This is an overloaded member function, provided for convenience.

Use setIcon() and setText() on the relevant QAction instead.

int QMenu::columns () const   [protected]

Use columnCount() instead.

bool QMenu::connectItem ( int id, const QObject * receiver, const char * member )

Use connect() on the relevant QAction instead.

uint QMenu::count () const

Use actions().count() instead.

bool QMenu::disconnectItem ( int id, const QObject * receiver, const char * member )

Use disconnect() on the relevant QAction instead.

QMenuItem * QMenu::findItem ( int id ) const

Use actions instead.

QMenuItem * QMenu::findPopup ( QMenu * popup, int * index )

Use QAction and actions() instead.

int QMenu::frameWidth () const

Use style()->pixelMetric(QStyle::PM_MenuPanelWidth, this) instead.

void QMenu::highlighted ( int itemId )   [signal]

Use hovered() instead.

QIcon QMenu::iconSet ( int id ) const

Use icon() on the relevant QAction instead.

int QMenu::idAt ( int index ) const

Use actions instead.

int QMenu::indexOf ( int id ) const

Use actions().indexOf(action) on the relevant QAction instead.

int QMenu::insertItem ( const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0, int id = -1, int index = -1 )

Use insertAction() or one of the addAction() overloads instead.

int QMenu::insertItem ( const QIcon & icon, const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertAction() or one of the addAction() overloads instead.

int QMenu::insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertAction() or one of the addAction() overloads instead.

int QMenu::insertItem ( const QString & text, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertAction() or one of the addAction() overloads instead.

int QMenu::insertItem ( const QIcon & icon, const QString & text, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertAction() or one of the addAction() overloads instead.

int QMenu::insertItem ( const QString & text, QMenu * popup, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertMenu() or one of the addMenu() overloads instead.

int QMenu::insertItem ( const QIcon & icon, const QString & text, QMenu * popup, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertMenu() or one of the addMenu() overloads instead.

int QMenu::insertItem ( const QPixmap & pixmap, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertAction() or one of the addAction() overloads instead.

int QMenu::insertItem ( const QPixmap & pixmap, QMenu * popup, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertMenu() or one of the addMenu() overloads instead.

int QMenu::insertItem ( QMenuItem * item, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience.

Use insertAction() or one of the addAction() overloads instead.

int QMenu::insertSeparator ( int index = -1 )

This is an overloaded member function, provided for convenience.

Use the insertSeparator() overload that takes a QAction * parameter instead.

int QMenu::insertTearOffHandle ( int a = 0, int b = 0 )

Use setTearOffEnabled() instead.

bool QMenu::isCheckable () const

Not necessary anymore. Always returns true.

bool QMenu::isItemActive ( int id ) const

Use activeAction() instead.

bool QMenu::isItemChecked ( int id ) const

Use isChecked() on the relevant QAction instead.

bool QMenu::isItemEnabled ( int id ) const

Use isEnabled() on the relevant QAction instead.

bool QMenu::isItemVisible ( int id ) const

Use isVisible() on the relevant QAction instead.

int QMenu::itemAtPos ( const QPoint & p, bool ignoreSeparator = true )   [protected]

Use actions instead.

QFont QMenu::itemFont ( int id ) const

Use font() on the relevant QAction instead.

See also setItemFont().

QRect QMenu::itemGeometry ( int index )

Use actionGeometry() on the relevant QAction instead.

int QMenu::itemHeight ( int index )   [protected]

Use actionGeometry(actions().value(index)).height() instead.

int QMenu::itemHeight ( QMenuItem * mi )   [protected]

This is an overloaded member function, provided for convenience.

Use actionGeometry() instead.

int QMenu::itemParameter ( int id ) const

Use QAction::data() instead.

See also setItemParameter().

QPixmap QMenu::pixmap ( int id ) const

Use QPixmap(icon()) on the relevant QAction instead.

void QMenu::popup ( const QPoint & pos, int indexAtPoint )

This is an overloaded member function, provided for convenience.

Use popup() on the relevant QAction instead.

void QMenu::removeItem ( int id )

Use removeAction() instead.

void QMenu::removeItemAt ( int index )

Use removeAction() instead.

void QMenu::setAccel ( const QKeySequence & key, int id )

Use setShortcut() on the relevant QAction instead.

See also accel().

void QMenu::setActiveItem ( int id )

Use setActiveAction() instead.

void QMenu::setCheckable ( bool checkable )

Not necessary anymore. The checkable parameter is ignored.

See also isCheckable().

void QMenu::setId ( int index, int id )

Use actions instead.

void QMenu::setItemChecked ( int id, bool check )

Use setChecked() on the relevant QAction instead.

See also isItemChecked().

void QMenu::setItemEnabled ( int id, bool enable )

Use setEnabled() on the relevant QAction instead.

See also isItemEnabled().

void QMenu::setItemFont ( int id, const QFont & font )

Use setFont() on the relevant QAction instead.

See also itemFont().

bool QMenu::setItemParameter ( int id, int param )

Use QAction::setData() instead.

See also itemParameter().

void QMenu::setItemVisible ( int id, bool visible )

Use setVisible() on the relevant QAction instead.

See also isItemVisible().

void QMenu::setWhatsThis ( int id, const QString & w )

Use setWhatsThis() on the relevant QAction instead.

See also whatsThis().

QString QMenu::text ( int id ) const

Use text() on the relevant QAction instead.

QString QMenu::whatsThis ( int id ) const

Use whatsThis() on the relevant QAction instead.

See also setWhatsThis().


Copyright © 2007 Trolltech Trademarks
Qt 4.2.3