QWidgetAction Class

QWidgetActionQAction 的子类, 增加一个扩展接口, 将自定义widget插入action容器, 如工具栏. 更多...

头文件: #include <QWidgetAction>
qmake: QT += widgets
开始支持版本: Qt 4.2
基类: QAction

公有函数

QWidgetAction(QObject *parent)
virtual ~QWidgetAction()
QWidget *defaultWidget() const
void releaseWidget(QWidget *widget)
QWidget *requestWidget(QWidget *parent)
void setDefaultWidget(QWidget *widget)
  • 47 个公有函数继承自 QAction
  • 32 个公有函数继承自 QObject

受保护的函数

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

重新实现的受保护函数

virtual bool event(QEvent *event)
virtual bool eventFilter(QObject *obj, QEvent *event)
  • 1 个受保护的函数继承自 QAction
  • 9 个受保护的函数继承自 QObject

其他继承的成员

详细描述

QWidgetActionQAction 的子类, 增加一个扩展接口, 将自定义widget插入action容器, 如工具栏.

应用程序的多数操作是通过菜单或工具栏的按钮. 然而有时也需要更复杂的widget. 如, 文字处理程序中的缩放操作可能在QToolBar中放置 QComboBox 实现, QComboBox 列出不同的缩放级别. QToolBar 提供 QToolBar::insertWidget() 函数插入widget. 但是, 如果你想实现一个action, 它用于多容器中显示的自定义widget, 这时你必须子类化 QWidgetAction.

如果你想在QToolBar 增加 QWidgetAction, 则调用 QWidgetAction::createWidget() 函数. 这个函数使用父对象创建一个自定义widget.

如果从容器widget删除action, 你应该调用 QWidgetAction::deleteWidget() 函数,并将之前创建的widget作为参数传入. 这个函数的默认实现是隐藏widget, 并调用 QObject::deleteLater()函数.

如果你仅有一个自定义widget, 那么你可以调用 setDefaultWidget()将其设置为默认widget. 这个widget会在action加入 QToolBar时用到, 或加入支持 QWidgetAction的容器中. 如果一个默认widget的 QWidgetAction 同时加到两个工具栏中, 那么这个widget会显示在第一个工具栏上. QWidgetAction 被默认widget接管所有权.

注意: 这个action由widget激活, 如重新实现鼠标事件调用 QAction::trigger().

macOS: 在 macOS上, 如果你想将widget加入应用程序的菜单栏, 这个widget可以加入并正常工作, 但是有一些限制:

  1. 这个widget的父对象从 QMenu 转移给系统菜单视图. 如果你想在其他地方显示菜单 (如. 弹出式菜单), 这个widget将不存在.
  2. 无法使用焦点/键盘事件.
  3. 由于Apple公司的设计, widget的鼠标追踪功能不生效.
  4. 连接 triggered() 信号的槽函数, 在 macOS 10.4 上打开模态对话框时会崩溃(Apple已确认bug), 解决方式是连接方式采用QueuedConnection替代DirectConnection.

参见 QAction, QActionGroup, QWidget.

成员函数

QWidgetAction::QWidgetAction(QObject *parent)

Constructs an action with parent.

[virtual] QWidgetAction::~QWidgetAction()

Destroys the object and frees allocated resources.

[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

Returns the default 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().

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

Reimplemented from QObject::event().

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

Reimplemented from QObject::eventFilter().

void QWidgetAction::releaseWidget(QWidget *widget)

Releases the specified widget.

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

参见 requestWidget(), deleteWidget(), and 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(), and defaultWidget().

void QWidgetAction::setDefaultWidget(QWidget *widget)

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