Most actions in an 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
QToolBar, presenting a range of different zoom levels.
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
If the action is removed from a container widget then
deleteWidget()is called with the previously created custom widget as argument. The default implementation hides the widget and deletes it using
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
QWidgetActionwith 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.
QWidgetActiontakes 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
macOS : If you add a widget to a menu in the application’s menu bar on macOS, the widget will be added and it will function but with some limitations:
The widget is reparented away from the
QMenuto the native menu view. If you show the menu in some other place (e.g. as a popup menu), the widget will not be there.
Focus/Keyboard handling of the widget is not possible.
Due to Apple’s design, mouse tracking on the widget currently does not work.
triggered()signal to a slot that opens a modal dialog will cause a crash in macOS 10.4 (known bug acknowledged by Apple), a workaround is to use a QueuedConnection instead of a DirectConnection.
- class PySide2.QtWidgets.QWidgetAction(parent)¶
- param parent:
Constructs an action with
This function is called whenever the action is added to a container widget that supports custom widgets. If you don’t want a custom widget to be used as representation of the action in the specified
parentwidget then 0 should be returned.
- Return type:
Returns the list of widgets that have been using
createWidget()and are currently in use by widgets the action has been added to.
- Return type:
Returns the default widget.
This function is called whenever the action is removed from a container widget that displays the action using a custom
widgetpreviously created using
createWidget(). The default implementation hides the
widgetand schedules it for deletion using
Releases the specified
Container widgets that support actions call this function when a widget action is removed.
Returns a widget that represents the action, with the given
Container widgets that support actions can call this function to request a widget as visual representation of the action.
widgetto be the default widget. The ownership is transferred to
createWidget()is reimplemented by a subclass to return a new widget the default widget is used when a container widget requests a widget through
© 2022 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 the GNU 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.