Qt Toolkit - QPopupMenu Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QPopupMenu Class Reference

The QPopupMenu class provides a popup menu widget. More...

`#include <qpopupmenu.h>`

Inherits QFrame and QMenuData.

List of all member functions.

## Public Members

- QPopupMenu ( QWidget * parent=0, const char * name=0 )
- ~QPopupMenu ()
- void popup ( const QPoint & pos, int indexAtPoint = 0 )
- virtual void updateItem ( int id )
- virtual void setCheckable ( bool )
- bool isCheckable () const
- int exec ()
- int exec ( const QPoint & pos, int indexAtPoint = 0 )
- virtual void setActiveItem ( int )
- int idAt ( int index ) const
- int idAt ( const QPoint & pos ) const
- int insertTearOffHandle ( int id=-1, int index=-1 )

## Signals

- void activated ( int itemId )
- void highlighted ( int itemId )
- void activatedRedirect ( int itemId ) (internal)
- void highlightedRedirect ( int itemId ) (internal)
- void aboutToShow ()
- void aboutToHide ()

## Important Inherited Members

- int insertItem ( QCustomMenuItem * custom, int id, int index )
- int insertItem ( QWidget * widget, int id, int index )
- int insertItem ( const QIconSet & icon, QCustomMenuItem * custom, int id, int index )
- int insertItem ( const QIconSet & icon, const QPixmap & pixmap, QPopupMenu * popup, int id, int index )
- int insertItem ( const QIconSet & icon, const QPixmap & pixmap, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QIconSet & icon, const QPixmap & pixmap, int id, int index )
- int insertItem ( const QIconSet & icon, const QString & text, QPopupMenu * popup, int id, int index )
- int insertItem ( const QIconSet & icon, const QString & text, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QIconSet & icon, const QString & text, int id, int index )
- int insertItem ( const QPixmap & pixmap, QPopupMenu * popup, int id, int index )
- int insertItem ( const QPixmap & pixmap, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QPixmap & pixmap, int id, int index )
- int insertItem ( const QString & text, QPopupMenu * popup, int id, int index )
- int insertItem ( const QString & text, const QObject * receiver, const char * member, int accel, int id, int index )
- int insertItem ( const QString & text, int id, int index )
- void removeItem ( int id )
- void removeItemAt ( int index )
- void clear ()
- QString text ( int id ) const
- QPixmap* pixmap ( int id ) const
- QIconSet* iconSet ( int id ) const
- int insertSeparator ( int index )
- void changeItem ( int id, const QIconSet & icon, const QPixmap & pixmap )
- void changeItem ( int id, const QIconSet & icon, const QString & text )
- void changeItem ( int id, const QPixmap & pixmap )
- void changeItem ( int id, const QString & text )
- QString whatsThis ( int id ) const
- void setWhatsThis ( int id, const QString & text )
- int accel ( int id ) const
- void setAccel ( int key, int id )
- void setItemEnabled ( int id, bool enable )
- bool isItemEnabled ( int id ) const
- void setItemChecked ( int id, bool check )
- bool isItemChecked ( int id ) const
- bool connectItem ( int id, const QObject * receiver, const char * member )
- bool disconnectItem ( int id, const QObject * receiver, const char * member )
- bool setItemParameter ( int id, int param )
- int itemParameter ( int id ) const

## Protected Members

- int itemHeight ( int ) const
- int itemHeight ( QMenuItem * mi ) const
- void drawItem ( QPainter * p, int tab, QMenuItem * mi, bool act, int x, int y, int w, int h )
- virtual void drawContents ( QPainter * )
- int columns () const

## Properties

Type | Name | READ | WRITE | Options
--- | --- | --- | --- | ---
bool | checkable | isCheckable | setCheckable |

## Detailed Description

The QPopupMenu class provides a popup menu widget.

A popup menu widget is a selection menu. It can be both, 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 hits the specified shortcut key. Use QMenuBar::insertItem() to insert a popup menu into a menu bar. Show a context menu either asynchronously with popup() or synchronously with exec().

Technically, a popup menu consists of a list of menu items. You add items with insertItem(). An item is either a string, a pixmap or a custom item that provides its own drawing function (see QCustomMenuItem). In addition, items can have an optional icon drawn on the very left side and an accelerator key, like "Ctrl-X".

There are three kind of menu items: separators, those that perform an action and those that show a submenu. Separators are inserted with insertSeparator(). For submenus, you pass a pointer to a QPopupMenu in your call to insertItem(). All other items are considered action items.

When inserting actions items, you usually specify a receiver and a slot. The receiver will be notified whenever the item was selected. In addition, QPopupMenu provides two signals activated() and highlighted() that signal the identifier of the respective menu item. Sometimes it is practical to connect several items to one slot. To distinguish between them, specify a slot that takes an integer argument and use setItemParameter() to associate a unique value with each item.

You clear a popup menu with clear() and remove single items with removeItem() or removeItemAt().

A popup menu can display check marks for certain items when enabled with setCheckable(TRUE). You check or uncheck items with setItemChecked().

Items are either enabled or disabled. You toggle their state with setItemEnabled(). Just before a popup menu becomes visible, it emits the aboutToShow() signal. You can use this signal to set the correct enabled/disabled states of all menu items before the user sees it. The corresponding aboutToHide() signal is emitted when the menu hides again.

You can provide What's This? help for single menu items with setWhatsThis(). See QWhatsThis for general information about this kind of light-weight online help.

For ultimate flexibility, you can also add entire widgets as items into a popup menu, for example a color selector.

A QPopupMenu can also provide a tear-off menu. A tear-off menu is a "torn off" copy of a menu that lives in a separate window. 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 that functionality for a certain menu, insert a tear-off handle with insertTearOffHandle(). When using tear-off menus, keep in mind that the concept isn't typically used on MS-Windows, so users may not be familiar with it. Consider using a QToolBar instead.

menu/menu.cpp is a typical example of QMenuBar and QPopupMenu use.

See also QMenuBar and GUI Design Handbook: Menu, Drop-Down and Pop-Up

Examples: qtimage/qtimage.cpp grapher/grapher.cpp mainlyQt/editor.cpp layout/layout.cpp menu/menu.cpp progress/progress.cpp scrollview/scrollview.cpp

## Member Function Documentation

### QPopupMenu::QPopupMenu ( QWidget * parent=0, const char * name=0 )

Constructs a popup menu with a parent and a widget name.

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

### QPopupMenu::~QPopupMenu ()

Destructs the popup menu.

### void QPopupMenu::aboutToHide () `[signal]`

This signal is emitted just before the popup menu is hidden after it has been displayed.

See also aboutToShow(), setItemEnabled(), setItemChecked(), insertItem() and removeItem().

### void QPopupMenu::aboutToShow () `[signal]`

This signal is emitted just before the popup menu is displayed. You can connect it to any slot that sets up the menu contents (e.g. to ensure that the right items are enabled).

See also aboutToHide(), setItemEnabled(), setItemChecked(), insertItem() and removeItem().

### void QPopupMenu::activateItemAt ( int index )

Reimplemented for internal reasons; the API is not affected.

### void QPopupMenu::activated ( int id ) `[signal]`

This signal is emitted when a menu item is selected; id is the id of the selected item.

Normally, you will connect each menu item to a single slot using QMenuData::insertItem(), but sometimes you will want to connect several items to a single slot (most often if the user selects from an array). This signal is handy in such cases.

See also highlighted() and QMenuData::insertItem().

### void QPopupMenu::closeEvent ( QCloseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### int QPopupMenu::columns () const `[protected]`

If a popup menu does not fit on the screen, it layouts itself in multiple columns until it fits.

This functions returns in how many.

See also sizeHint().

### bool QPopupMenu::customWhatsThis () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::drawContents ( QPainter * p ) `[virtual protected]`

Draws all menu items.

Reimplemented from QFrame.

### void QPopupMenu::drawItem ( QPainter * p, int tab_, QMenuItem * mi, bool act, int x, int y, int w, int h ) `[protected]`

Draws item mi in the area x, y, w, h, using painter p. The item is drawn active or inactive according to act, and using the rightmost tab_ pixels for accelerator text.

See also QStyle::drawPopupMenuItem().

### int QPopupMenu::exec ()

Execute this popup synchronously.

Similar to the above function, but the position of the popup is not set, so you must choose an appropriate position. The function move the popup if it is partially off-screen.

More common usage is to position the popup at the current mouse position:

          exec(QCursor::pos());

or aligned to a widget:

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

### int QPopupMenu::exec ( const QPoint & pos, int indexAtPoint = 0 )

Execute this popup synchronously.

Opens the popup menu so that the item number indexAtPoint will be at the specified global position pos. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().

The return code is the ID of the selected item in either the popup menu or one of its submenus, or -1 if no item is selected (normally because the user presses Escape).

Note that all signals are emitted as usual. If you connect a menu item 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 popup at the current mouse position:

          exec(QCursor::pos());

or aligned to a widget:

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

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

See also popup() and sizeHint().

### void QPopupMenu::focusInEvent ( QFocusEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### bool QPopupMenu::focusNextPrevChild ( bool next ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::focusOutEvent ( QFocusEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::hide () `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::highlighted ( int id ) `[signal]`

This signal is emitted when a menu item is highlighted; id is the id of the highlighted item.

Normally, you will connect each menu item to a single slot using QMenuData::insertItem(), but sometimes you will want to connect several items to a single slot (most often if the user selects from an array). This signal is handy in such cases.

See also activated() and QMenuData::insertItem().

### int QPopupMenu::idAt ( const QPoint & pos ) const

Return the id of the item at pos, or -1 if there is no item there, or if it is a separator item.

Examples: scrollview/scrollview.cpp

### int QPopupMenu::idAt ( int index ) const

Returns the identifier of the menu item at position index in the internal list, or -1 if index is out of range.

See also QMenuData::setId() and QMenuData::indexOf().

### int QPopupMenu::insertTearOffHandle ( int id=-1, int index=-1 )

Inserts a tear-off handle into the menu. A tear-off handle is a special menu item, that - when selected - creates a copy of the menu. This "torn off" copy lives in a separate window. It contains the same choices as the original menu, with the exception of the tear-off handle.

You may also want to set a proper window title for the tear-off menu with setCaption().

The handle item is assigned the identifier id or an automatically generated identifier if id is < 0. The generated identifiers (negative integers) are guaranteed to be unique within the entire application.

The index specifies the position in the menu. The tear-off handle is appended at the end of the list if index is negative.

Examples: menu/menu.cpp

### bool QPopupMenu::isCheckable () const

Returns whether display of check marks by the menu items is enabled.

See also setCheckable() and QMenuData::setItemChecked().

### int QPopupMenu::itemHeight ( QMenuItem * mi ) const `[protected]`

Calculates the height in pixels of the item mi.

### int QPopupMenu::itemHeight ( int row ) const `[protected]`

Calculates the height in pixels of the item in row row.

### void QPopupMenu::keyPressEvent ( QKeyEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::mouseMoveEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::mousePressEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::mouseReleaseEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::paintEvent ( QPaintEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::popup ( const QPoint & pos, int indexAtPoint = 0 )

Opens the popup menu so that the item number indexAtPoint will be at the specified global position pos. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().

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

Examples: qtimage/qtimage.cpp

### void QPopupMenu::setActiveItem ( int i ) `[virtual]`

Sets the currently active item to i and repaints as necessary.

### void QPopupMenu::setCheckable ( bool enable ) `[virtual]`

Enables or disables display of check marks by the menu items.

Notice that checking is always enabled when in windows-style.

See also isCheckable() and QMenuData::setItemChecked().

Examples: menu/menu.cpp progress/progress.cpp scrollview/scrollview.cpp

### void QPopupMenu::setFont ( const QFont & font ) `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::show () `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### QSize QPopupMenu::sizeHint () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::styleChange ( QStyle & old ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QPopupMenu::timerEvent ( QTimerEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QObject.

### void QPopupMenu::updateItem ( int id ) `[virtual]`

Updates the item with identity id.

Reimplemented from QMenuData.

### void QPopupMenu::activatedRedirect ( int id ) `[signal]`

For internal use only.

### void QPopupMenu::highlightedRedirect ( int id ) `[signal]`

For internal use only.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.