Qtopia Home - Classes - Hierachy - Annotated - Functions - Licenses - Reference

QPopupMenu Class Reference

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

#include <qpopupmenu.h>

Inherits QFrame and QMenuData.

Inherited by CategoryMenu and ContextMenu.

List of all member functions.

Public Members

Signals

Important Inherited Members

Properties

Protected Members


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.


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.

Warning: Do not open a widget in a slot connected to this signal.

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

int QMenuData::accel ( int id ) const

Returns the accelerator key that has been defined for the menu item id, or 0 if it has no accelerator key.

See also setAccel(), QAccel, and qnamespace.h.

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 QMenuData::changeItem ( int id, const QString & text )

Changes the text of the menu item id. If the item has an icon, the icon remains unchanged.

See also text().

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

Changes the pixmap of the menu item id. If the item has an icon, the icon remains unchanged.

See also pixmap().

void QMenuData::changeItem ( int id, const QIconSet & icon, const QString & text )

Changes the icon and text of the menu item id.

See also pixmap().

void QMenuData::changeItem ( int id, const QIconSet & icon, const QPixmap & pixmap )

Changes the icon and pixmap of the menu item id.

See also pixmap().

void QMenuData::clear ()

Removes all menu items.

See also removeItem() and removeItemAt().

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 QMenuData::connectItem ( int id, const QObject * receiver, const char * member )

Connects a menu item to a receiver and a slot or signal.

The receiver's slot/signal is activated when the menu item is activated.

See also disconnectItem() and setItemParameter().

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

Disconnects a receiver/member from a menu item.

All connections are removed when the menu data object is destroyed.

See also connectItem() and setItemParameter().

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

QIconSet * QMenuData::iconSet ( int id ) const

Returns the icon set that has been set for menu item id, or 0 if no icon set has been set.

See also changeItem(), text(), and pixmap().

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

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

The family of insertItem() functions inserts menu items into a popup menu or a menu bar.

A menu item is usually either a text string or a a pixmap, both with an optional icon or keyboard accelerator. As special cases it is also possible to insert custom items (see QCustomMenuItem) or even widgets into popup menus.

Some insertItem() members take a popup menu as additional argument. Use these to insert submenus to existing menus or pulldown menus to a menu bar.

The amount of insert functions may look confusing, but is actually quite handy to use.

This default version inserts a menu item with a text, an accelerator key, an id and an optional index and connects it to an object/slot.

Example:

    QMenuBar   *mainMenu = new QMenuBar;
    QPopupMenu *fileMenu = new QPopupMenu;
    fileMenu->insertItem( "New",  myView, SLOT(newFile()), CTRL+Key_N );
    fileMenu->insertItem( "Open", myView, SLOT(open()),    CTRL+Key_O );
    mainMenu->insertItem( "File", fileMenu );
  

Not all insert functions take an object/slot parameter or an accelerator key. Use connectItem() and setAccel() on these items.

If you will need to translate accelerators, use QAccel::stringToKey() to calculate the accelerator key:

    fileMenu->insertItem( tr("Open"), myView, SLOT(open()),
                          QAccel::stringToKey( tr("Ctrl+O") ) );
  

In the example above, pressing CTRL+N or selecting "open" from the menu activates the myView->open() function.

Some insert functions take a QIconSet parameter to specify the little menu item icon. Note that you can always pass a QPixmap object instead.

The menu 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 menu item is appended at the end of the list if index is negative.

Note that keyboard accelerators in Qt are not application global, but bound to a certain toplevel window. Accelerators in QPopupMenu items therefore only work for menus that are associated with a certain window. This is true for popup menus that live in a menu bar, for instance. In that case, the accelerator will be installed on the menu bar itself. It also works for stand-alone popup menus that have a toplevel widget in their parentWidget()- chain. The menu will then install its accelerator object on that toplevel widget. For all other cases, use an independent QAccel object.

Warning: Be careful when passing a literal 0 to insertItem(), as some C++ compilers choose the wrong overloaded function. Cast the 0 to what you mean, eg. (QObject*)0.

See also removeItem(), changeItem(), setAccel(), connectItem(), QAccel, and qnamespace.h.

int QMenuData::insertItem ( const QIconSet & icon, const QString & text, const QObject * receiver, const char * member, int accel = 0, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with an icon, a text, an accelerator key, an id and an optional index and connects it to an object/slot. The icon will be displayed to the left of the text in the item.

See also removeItem(), changeItem(), setAccel(), connectItem(), QAccel, and qnamespace.h.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with a pixmap, an accelerator key, an id and an optional index and connects it to an object/slot.

To look best when being highlighted as menu item, the pixmap should provide a mask, see QPixmap::mask().

Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

int QMenuData::insertItem ( const QIconSet & icon, const QPixmap & pixmap, const QObject * receiver, const char * member, int accel = 0, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with an icon, a pixmap, an accelerator key, an id and an optional index and connects it to an object/slot. The icon will be displayed to the left of the pixmap in the item.

To look best when being highlighted as menu item, the pixmap should provide a mask, see QPixmap::mask().

Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), connectItem(), QAccel, and qnamespace.h.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with a text. Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with an icon and a text. The icon will be displayed to the left of the text in the item. Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with a text and a sub menu.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

int QMenuData::insertItem ( const QIconSet & icon, const QString & text, QPopupMenu * popup, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with an icon, a text and a sub menu. The icon will be displayed to the left of the text in the item.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with a pixmap. Returns the menu item identifier.

To look best when being highlighted as menu item, the pixmap should provide a mask, see QPixmap::mask().

See also removeItem(), changeItem(), setAccel(), and connectItem().

int QMenuData::insertItem ( const QIconSet & icon, const QPixmap & pixmap, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with an icon and a pixmap. The icon will be displayed to the left of the pixmap in the item. Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with a pixmap and a sub menu. The icon will be displayed to the left of the pixmap in the item.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

int QMenuData::insertItem ( const QIconSet & icon, const QPixmap & pixmap, QPopupMenu * popup, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item with an icon, a pixmap and a sub menu. The icon will be displayed to the left of the pixmap in the item.

The popup must be deleted by the programmer or by its parent widget. It is not deleted when this menu item is removed or when the menu is deleted.

Returns the menu item identifier.

See also removeItem(), changeItem(), setAccel(), and connectItem().

int QMenuData::insertItem ( QWidget * widget, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a menu item that consists of the widget widget.

Ownership of widget is transferred to the popup menu or the menubar.

Theoretically, any widget can be inserted into a popup menu. In practice, this only makes sense with certain widgets.

If a widget is not focus enabled ( see QWidget::isFocusEnabled() ), the menu treats it as a separator. This means, the item is not selectable and will never get focus. This way you can for example simply insert a QLabel if you need a popup menu with a title.

If the widget is focus enabled, it will get focus when the user traverses the popup menu with the arrow keys. If the widget does not accept ArrowUp and ArrowDown in its key event handler, the focus will move back to the menu when the the respective arrow key is hit one more time. This works for example with a QLineEdit. If the widget accepts the arrow keys itself, it must also provide the possibility to put the focus back on the menu again by calling QWidget::focusNextPrevChild() respectively. Futhermore should the embedded widget close the menu when the user made a selection. This can be done safely by calling

 if ( isVisible() &&
  parentWidget() &&
  parentWidget()->inherits("QPopupMenu") )
        parentWidget()->close();
  

See also removeItem().

int QMenuData::insertItem ( const QIconSet & icon, QCustomMenuItem * custom, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a custom menu item custom with an icon.

This only works with popup menus. It is not supported for menu bars. Ownership of custom is transferred to the popup menu.

If you want to connect a custom item to a certain slot, use connectItem().

See also connectItem(), removeItem(), and QCustomMenuItem.

int QMenuData::insertItem ( QCustomMenuItem * custom, int id = -1, int index = -1 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts a custom menu item custom.

This only works with popup menus. It is not supported for menu bars. Ownership of custom is transferred to the popup menu.

If you want to connect a custom item to a certain slot, use connectItem().

See also connectItem(), removeItem(), and QCustomMenuItem.

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

Inserts a separator at position index. The separator becomes the last menu item if index is negative.

In a popup menu, a separator is rendered as a horizontal line. In a Motif menubar, a separator is spacing, so the rest of the items (just "Help", normally) are drawn right-justified. In a Windows menubar, separators are ignored (to comply with the Windows style guide).

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.

bool QPopupMenu::isCheckable () const

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

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

bool QMenuData::isItemChecked ( int id ) const

Returns TRUE if the menu item has been checked, otherwise FALSE.

See also setItemChecked().

bool QMenuData::isItemEnabled ( int id ) const

Returns TRUE if the item with identifier id is enabled or FALSE if it is disabled.

See also setItemEnabled().

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

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

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

Calculates the height in pixels of the item mi.

int QMenuData::itemParameter ( int id ) const

Returns the parameter of the activation signal of item id.

If no parameter has been specified for this item with setItemParameter(), the value defaults to id.

See also connectItem(), disconnectItem(), and setItemParameter().

QPixmap * QMenuData::pixmap ( int id ) const

Returns the pixmap that has been set for menu item id, or 0 if no pixmap has been set.

See also changeItem(), text(), and iconSet().

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.

void QMenuData::removeItem ( int id )

Removes the menu item which has the identifier id.

See also removeItemAt() and clear().

void QMenuData::removeItemAt ( int index )

Removes the menu item at position index.

See also removeItem() and clear().

void QMenuData::setAccel ( int key, int id )

Defines an accelerator key for the menu item id.

An accelerator key consists of a key code and a combination of the modifiers SHIFT, CTRL, ALT, or UNICODE_ACCEL (OR'ed or added). The header file qnamespace.h contains a list of key codes.

Defining an accelerator key generates a text which is added to the menu item, for instance, CTRL + Key_O generates "Ctrl+O". The text is formatted differently for different platforms.

Note that keyboard accelerators in Qt are not application global, but bound to a certain toplevel window. Accelerators in QPopupMenu items therefore only work for menus that are associated with a certain window. This is true for popup menus that live in a menu bar, for instance. In that case, the accelerator will be installed on the menu bar itself. It also works for stand-alone popup menus that have a toplevel widget in their parentWidget()- chain. The menu will then install its accelerator object on that toplevel widget. For all other cases, use an independent QAccel object.

Example:

    QMenuBar   *mainMenu = new QMenuBar;
    QPopupMenu *fileMenu = new QPopupMenu;      // file sub menu
    fileMenu->insertItem( "Open Document", 67 );// add "Open" item
    fileMenu->setAccel( CTRL + Key_O, 67 );     // Control and O to open
    fileMenu->insertItem( "Quit", 69 );         // add "Quit" item
    fileMenu->setAccel( CTRL + ALT + Key_Delete, 69 );
    mainMenu->insertItem( "File", fileMenu );   // add the file menu
  

If you will need to translate accelerators, use QAccel::stringToKey():

    fileMenu->setAccel( QAccel::stringToKey(tr("Ctrl+O")), 67 );
  

You can also specify the accelerator in the insertItem() function.

See also accel(), insertItem(), QAccel, and qnamespace.h.

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

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

Checks the menu item with id id if check is TRUE, or unchecks it if check is FALSE, and calls QPopupMenu::setCheckable( TRUE ) if necessary.

See also isItemChecked().

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

Enables the menu item with identifier id if enable is TRUE, or disables the item if enable is FALSE.

See also isItemEnabled().

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

Sets the parameter of the activation signal of item id to param.

If any receiver takes an integer parameter, this value is passed.

See also connectItem(), disconnectItem(), and itemParameter().

void QMenuData::setWhatsThis ( int id, const QString & text )

Sets a Whats This help for a certain menu item.

\arg id is the menu item id. \arg text is the Whats This help text in rich text format ( see QStyleSheet)

See also whatsThis().

QString QMenuData::text ( int id ) const

Returns the text that has been set for menu item id, or a null string if no text has been set.

See also changeItem(), pixmap(), and iconSet().

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

Updates the item with identity id.

Reimplemented from QMenuData.

QString QMenuData::whatsThis ( int id ) const

Returns the Whats This help text for the specified item id or QString::null if no text has been defined yet.

See also setWhatsThis().


This file is part of the Qtopia platform, copyright © 1995-2005 Trolltech, all rights reserved.


Copyright © 2005 Trolltech Trademarks
Qtopia version 2.2.0