QWidgetAction 類

The QWidgetAction class extends QAction 通過界麵 (把自定義 Widget 插入基於動作的容器,譬如:工具欄)。 更多...

頭: #include <QWidgetAction>
qmake: QT += widgets
Since: Qt 4.2
繼承: QAction

公共函數

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

靜態公共成員

const QMetaObject staticMetaObject

保護函數

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

額外繼承成員

詳細描述

The QWidgetAction class extends QAction 通過界麵 (把自定義 Widget 插入基於動作的容器,譬如:工具欄)。

應用程序中的大多數動作被錶示成菜單項或工具欄按鈕。不管怎樣,更復雜的 Widget 有時是必要的。例如,單詞處理程序縮放動作可以被變現使用 QComboBox QToolBar ,呈現不同縮放級彆的範圍。 QToolBar 提供 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 .

QWidgetAction is added for example to a 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 .

成員函數文檔編製

QWidgetAction:: QWidgetAction ( QObject * parent )

構造動作采用 parent .

[虛擬] 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 ().

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

重實現自 QObject::eventFilter ().

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 )

返迴錶示動作的 Widget,采用給定 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 為默認 Widget。所有權會被轉移給 QWidgetAction 。除非 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 ().