QWidgetAction 类

QWidgetAction 类扩展 QAction 通过界面 (把自定义 Widget 插入基于动作的容器,譬如:工具栏)。 更多...

头: #include <QWidgetAction>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QAction

公共函数

QWidgetAction (QObject * parent )
virtual ~QWidgetAction ()
QWidget * defaultWidget () const
void releaseWidget (QWidget * widget )
QWidget * requestWidget (QWidget * parent )
void setDefaultWidget (QWidget * widget )

保护函数

virtual QWidget * createWidget (QWidget * parent )
QList<QWidget *> createdWidgets () const
virtual void deleteWidget (QWidget * widget )

重实现保护函数

virtual bool event (QEvent * event ) override
virtual bool eventFilter (QObject * obj , QEvent * event ) override

详细描述

应用程序中的大多数动作被表示成菜单项或工具栏按钮。不管怎样,更复杂的 Widget 有时是必要的。例如,单词处理程序缩放动作可以被变现使用 QComboBox QToolBar ,呈现不同缩放级别的范围。 QToolBar 提供 QToolBar::insertWidget () 作为用于插入单个 Widget 的方便函数。不管怎样,若想要在多个容器中使用自定义 Widget 实现可视化动作,则必须子类化 QWidgetAction。

例如:若 QWidgetAction 被添加到 QToolBar then QWidgetAction::createWidget () 被调用。该函数的重实现应该采用指定父级创建新的自定义 Widget。

若动作从容器 Widget 中被移除则 QWidgetAction::deleteWidget () 被调用采用先前创建的自定义 Widget 作为自变量。默认实现隐藏 Widget 并删除它使用 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 ().

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:

  1. The widget is reparented away from the QMenu to 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.
  2. Focus/Keyboard handling of the widget is not possible.
  3. Due to Apple's design, 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 macOS 10.4 (known bug acknowledged by Apple), a workaround is to use a QueuedConnection instead of a DirectConnection.

另请参阅 QAction , QActionGroup ,和 QWidget .

成员函数文档编制

[explicit] QWidgetAction:: QWidgetAction ( QObject * parent )

构造动作采用 parent .

[virtual noexcept] QWidgetAction:: ~QWidgetAction ()

销毁对象并释放分配的资源。

[virtual protected] QWidget *QWidgetAction:: createWidget ( QWidget * parent )

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 parent widget then 0 should be returned.

另请参阅 deleteWidget ().

[protected] QList < QWidget *> QWidgetAction:: createdWidgets () const

Returns the list of widgets that have been using createWidget () and are currently in use by widgets the action has been added to.

QWidget *QWidgetAction:: defaultWidget () const

返回默认 Widget。

另请参阅 setDefaultWidget ().

[virtual protected] void QWidgetAction:: deleteWidget ( QWidget * widget )

This function is called whenever the action is removed from a container widget that displays the action using a custom widget previously created using createWidget (). The default implementation hides the widget and schedules it for deletion using QObject::deleteLater ().

另请参阅 createWidget ().

[override virtual protected] bool QWidgetAction:: event ( QEvent * event )

重实现: QAction::event (QEvent *e).

[override virtual protected] bool QWidgetAction:: eventFilter ( QObject * obj , QEvent * event )

重实现: QObject::eventFilter (QObject *watched, QEvent *event).

void QWidgetAction:: releaseWidget ( QWidget * widget )

释放指定 widget .

Container widgets that support actions call this function when a widget action is removed.

另请参阅 requestWidget (), deleteWidget (),和 defaultWidget ().

QWidget *QWidgetAction:: requestWidget ( QWidget * parent )

Returns a widget that represents the action, with the given parent .

Container widgets that support actions can call this function to request a widget as visual representation of the action.

另请参阅 releaseWidget (), createWidget (),和 defaultWidget ().

void QWidgetAction:: setDefaultWidget ( QWidget * widget )

设置 widget to be the default widget. The ownership is transferred to QWidgetAction . Unless createWidget () is reimplemented by a subclass to return a new widget the default widget is used when a container widget requests a widget through requestWidget ().

另请参阅 defaultWidget ().