|  | Home · Overviews · Examples |

# QWidgetAction Class Reference
[com.trolltech.qt.gui module]

The `QWidgetAction` class extends `QAction` by an interface for inserting custom widgets into action based containers, such as toolbars. More...

Inherits `QAction`.

## Detailed Description

The `QWidgetAction` class extends `QAction` by an interface for inserting custom widgets into action based containers, such as toolbars.

Most actions in application are represented as items in menus or buttons in toolbars. However sometimes more complex widgets are necessary. For example a zoom action in a word processor may be realized using a `QComboBox` in a `QToolBar`, presenting a range of different zoom levels. `QToolBar` provides QToolBar::insertWidget() as convenience function for inserting a single widget. However if you want to implement an action that uses custom widgets for visualization in multiple containers then you have to subclass `QWidgetAction`.

If a `QWidgetAction` is added for example to a `QToolBar` then QWidgetAction::createWidget() is called. Reimplementations of that function should create a new custom widget with the specified parent.

If the action is removed from a container widget then QWidgetAction::deleteWidget() is called with the previously created custom widget as argument. The default implementation hides the widget and deletes it using QObject::deleteLater().

If you have only one single custom widget then you can set it as default widget using `setDefaultWidget`. That widget will then be used if the action is added to a `QToolBar`, or in general to an action container that supports `QWidgetAction`. If a `QWidgetAction` with only a default widget is added to two toolbars at the same time then the default widget is shown only in the first toolbar the action was added to. `QWidgetAction` takes over ownership of the default widget.

Note that it is up to the widget to activate the action, for example by reimplementing mouse event handlers and calling QAction::trigger().

Mac OS X: If you add widgets to a menu in the application's menu bar on Mac OS X, the widget will be added and function but there are some limitations:

1. The widget is reparented away from the `QMenu` to the native menu view. If you show the menu some other place (e.g. as a popup menu), the widget will not be there.
2. Focus/Keyboard handling of the widget is not possible
3. Mouse tracking on the widget currently does not work
4. Connecting the `triggered` signal to a slot that opens a modal dialog will cause a crash in Mac OS X 10.4 (known bug acknowledged by Apple), a workaround is to use a QueuedConnection instead of a DirectConnection.

See also `QAction`, `QActionGroup`, and `QWidget`.