QAction 类

QAction 类提供可以插入 Widget 的抽象用户界面动作。 更多...

头: #include <QAction>
qmake: QT += widgets
继承: QObject
继承者:

QWidgetAction

公共类型

enum ActionEvent { Trigger, Hover }
enum MenuRole { NoRole, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole, ..., QuitRole }
enum Priority { LowPriority, NormalPriority, HighPriority }

特性

公共函数

QAction (QObject * parent = nullptr)
QAction (const QString & text , QObject * parent = nullptr)
QAction (const QIcon & icon , const QString & text , QObject * parent = nullptr)
~QAction ()
QActionGroup * actionGroup () const
void activate (ActionEvent event )
QList<QGraphicsWidget *> associatedGraphicsWidgets () const
QList<QWidget *> associatedWidgets () const
bool autoRepeat () const
QVariant data () const
QFont font () const
QIcon icon () const
QString iconText () const
bool isCheckable () const
bool isChecked () const
bool isEnabled () const
bool isIconVisibleInMenu () const
bool isSeparator () const
bool isVisible () const
QMenu * menu () const
MenuRole menuRole () const
QWidget * parentWidget () const
优先级 priority () const
void setActionGroup (QActionGroup * group )
void setAutoRepeat ( bool )
void setCheckable ( bool )
void setData (const QVariant & userData )
void setFont (const QFont & font )
void setIcon (const QIcon & icon )
void setIconText (const QString & text )
void setIconVisibleInMenu (bool visible )
void setMenu (QMenu * menu )
void setMenuRole (MenuRole menuRole )
void setPriority (Priority priority )
void setSeparator (bool b )
void setShortcut (const QKeySequence & shortcut )
void setShortcutContext (Qt::ShortcutContext context )
void setShortcuts (const QList<QKeySequence> & shortcuts )
void setShortcuts (QKeySequence::StandardKey key )
void setStatusTip (const QString & statusTip )
void setText (const QString & text )
void setToolTip (const QString & tip )
void setWhatsThis (const QString & what )
QKeySequence shortcut () const
Qt::ShortcutContext shortcutContext () const
QList<QKeySequence> shortcuts () const
bool showStatusText (QWidget * widget = Q_NULLPTR)
QString statusTip () const
QString text () const
QString toolTip () const
QString whatsThis () const

公共槽

void hover ()
void setChecked ( bool )
void setDisabled (bool b )
void setEnabled ( bool )
void setVisible ( bool )
void toggle ()
void trigger ()

信号

void changed ()
void hovered ()
void toggled (bool checked )
void triggered (bool checked = false)

重实现保护函数

virtual bool event (QEvent * e )

额外继承成员

详细描述

QAction 类提供可以插入 Widget 的抽象用户界面动作。

在应用程序中,许多常见命令可以凭借菜单、工具栏按钮及键盘快捷键援引。由于用户期望每个命令以相同方式履行,不管所用用户界面,它是有用的将每个命令表示成 action .

动作可以被添加到菜单和工具栏,并自动保持同步。例如,在单词处理程序中,若用户按下加粗工具栏按钮,加粗菜单项将被自动选中。

可以将动作创建成独立对象,但也可以在构造菜单期间创建; QMenu 类包含用于创建适合用作菜单项的动作的方便函数。

A QAction 可能包含图标、菜单文本、快捷方式、状态文本、What's This? 文本及工具提示。这些中的大多数可以在构造函数中设置。也可以独立设置它们采用 setIcon (), setText (), setIconText (), setShortcut (), setStatusTip (), setWhatsThis (),和 setToolTip ()。对于菜单项,可以设置单个字体采用 setFont ().

动作被添加到 Widget 使用 QWidget::addAction () 或 QGraphicsWidget::addAction ()。注意:动作必须被添加到 Widget 在使用它之前;这也为 true 当快捷方式应该全局时 (即 Qt::ApplicationShortcut as Qt::ShortcutContext ).

一旦 QAction 被创建,就应把它添加到相关菜单和工具栏,然后连接到将履行动作的槽。例如:

    const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
    QAction *openAct = new QAction(openIcon, tr("&Open..."), this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setStatusTip(tr("Open an existing file"));
    connect(openAct, &QAction::triggered, this, &MainWindow::open);
    fileMenu->addAction(openAct);
    fileToolBar->addAction(openAct);
					
fileMenu->addAction(openAct);
					

推荐将动作创建作为在其中使用它们的窗口子级。在大多数情况下,动作将是应用程序主窗口的子级。

另请参阅 QMenu , QToolBar ,和 应用程序范例 .

成员类型文档编制

enum QAction:: ActionEvent

使用此枚举类型当调用 QAction::activate ()

常量 描述
QAction::Trigger 0 这将导致 QAction::triggered () 信号被发射。
QAction::Hover 1 这将导致 QAction::hovered () 信号被发射。

此枚举描述应该如何把动作移入 macOS 应用程序菜单。

常量 描述
QAction::NoRole 0 不应将此动作放入应用程序菜单
QAction::TextHeuristicRole 1 This action should be put in the application menu based on the action's text as described in the QMenuBar 文档编制。
QAction::ApplicationSpecificRole 2 应按应用程序特定角色,将此动作放入应用程序菜单
QAction::AboutQtRole 3 此动作处理 About Qt 菜单项。
QAction::AboutRole 4 This action should be placed where the "About" menu item is in the application menu. The text of the menu item will be set to "About <application name>". The application name is fetched from the Info.plist file in the application's bundle (See Qt for macOS - 部署 ).
QAction::PreferencesRole 5 This action should be placed where the "Preferences..." menu item is in the application menu.
QAction::QuitRole 6 This action should be placed where the Quit menu item is in the application menu.

Setting this value only has effect on items that are in the immediate menus of the menubar, not the submenus of those menus. For example, if you have File menu in your menubar and the File menu has a submenu, setting the MenuRole for the actions in that submenu have no effect. They will never be moved.

enum QAction:: Priority

此枚举定义用户界面中动作的优先级。

常量 描述
QAction::LowPriority 0 动作不应该在用户界面中被优先。
QAction::NormalPriority 128
QAction::HighPriority 256 动作应该在用户界面中被优先。

该枚举在 Qt 4.6 引入或被修改。

另请参阅 priority .

特性文档编制

autoRepeat : bool

此特性保持动作是否可以自动重复

若为 true,动作会自动重复当按下键盘快捷方式组合时,前提是系统启用了键盘自动重复。默认值为 true。

该特性在 Qt 4.2 引入。

访问函数:

bool autoRepeat () const
void setAutoRepeat ( bool )

通知程序信号:

void changed ()

checkable : bool

此特性保持动作是否为可复选动作

A checkable action is one which has an on/off state. For example, in a word processor, a Bold toolbar button may be either on or off. An action which is not a toggle action is a command action; a command action is simply executed, e.g. file save. By default, this property is false .

In some situations, the state of one toggle action should depend on the state of others. For example, "Left Align", "Center" and "Right Align" toggle actions are mutually exclusive. To achieve exclusive toggling, add the relevant toggle actions to a QActionGroup 采用 QActionGroup::exclusive property set to true.

访问函数:

bool isCheckable () const
void setCheckable ( bool )

通知程序信号:

void changed ()

另请参阅 QAction::setChecked ().

checked : bool

此特性保持动作是否被复选。

仅可复选动作可以被复选。默认情况下,这为 false (动作未被复选)。

访问函数:

bool isChecked () const
void setChecked ( bool )

通知程序信号:

void toggled (bool checked )

另请参阅 checkable .

enabled : bool

此特性保持动作是否被启用

Disabled actions cannot be chosen by the user. They do not disappear from menus or toolbars, but they are displayed in a way which indicates that they are unavailable. For example, they might be displayed using only shades of gray.

What's This? help on disabled actions is still available, provided that the QAction::whatsThis property is set.

An action will be disabled when all widgets to which it is added (with QWidget::addAction ()) are disabled or not visible. When an action is disabled, it is not possible to trigger it through its shortcut.

默认情况下此特性为 true (动作被启用)。

访问函数:

bool isEnabled () const
void setEnabled ( bool )

通知程序信号:

void changed ()

另请参阅 text .

font : QFont

此特性保持动作的字体

字体特性用于渲染文本设置在 QAction 。字体被认为是提示,因为在所有情况下都不会基于应用程序和样式咨询字体。

默认情况下,此特性包含应用程序默认字体。

访问函数:

QFont font () const
void setFont (const QFont & font )

通知程序信号:

void changed ()

另请参阅 QAction::setText () 和 QStyle .

icon : QIcon

此特性保持动作的图标

在工具栏中,图标被用作工具按钮图标;在菜单中,它显示在菜单文本左侧。不存在默认图标。

若 null 图标 ( QIcon::isNull ()) 被传入此函数,动作图标被清零。

访问函数:

QIcon icon () const
void setIcon (const QIcon & icon )

通知程序信号:

void changed ()

iconText : QString

此特性保持动作的描述性图标文本

QToolBar::toolButtonStyle is set to a value that permits text to be displayed, the text defined held in this property appears as a label in the relevant tool button.

It also serves as the default text in menus and tooltips if the action has not been defined with setText () 或 setToolTip (), and will also be used in toolbar buttons if no icon has been defined using setIcon ().

若未明确设置图标文本,动作的正常文本将用于图标文本。

默认情况下,此特性包含空字符串。

访问函数:

QString iconText () const
void setIconText (const QString & text )

通知程序信号:

void changed ()

另请参阅 setToolTip () 和 setStatusTip ().

iconVisibleInMenu : bool

此特性保持动作是否应在菜单中展示图标

In some applications, it may make sense to have actions with icons in the toolbar, but not in menus. If true, the icon (if valid) is shown in the menu, when it is false, it is not shown.

The default is to follow whether the Qt::AA_DontShowIconsInMenus attribute is set for the application. Explicitly settings this property overrides the presence (or abscence) of the attribute.

例如:

QApplication app(argc, argv);
app.setAttribute(Qt::AA_DontShowIconsInMenus);  // Icons are *no longer shown* in menus
// ...
QAction *myAction = new QAction();
// ...
myAction->setIcon(SomeIcon);
myAction->setIconVisibleInMenu(true);   // Icon *will* be shown in menus for *this* action.
					

该特性在 Qt 4.4 引入。

访问函数:

bool isIconVisibleInMenu () const
void setIconVisibleInMenu (bool visible )

通知程序信号:

void changed ()

另请参阅 QAction::icon and QCoreApplication::setAttribute ().

此特性保持动作的菜单角色

This indicates what role the action serves in the application menu on macOS. By default all actions have the TextHeuristicRole , which means that the action is added based on its text (see QMenuBar for more information).

The menu role can only be changed before the actions are put into the menu bar in macOS (usually just before the first application window is shown).

该特性在 Qt 4.2 引入。

访问函数:

MenuRole menuRole () const
void setMenuRole (MenuRole menuRole )

通知程序信号:

void changed ()

priority : Priority

此特性保持在用户界面中动作的优先级。

可以设置此特性以指示动作在用户界面中的优先级应该如何。

对于实例,当工具栏拥有 Qt::ToolButtonTextBesideIcon 模式设置,那么动作具有 LowPriority 被不展示文本标签。

该特性在 Qt 4.6 引入。

访问函数:

优先级 priority () const
void setPriority (Priority priority )

shortcut : QKeySequence

此特性保持动作的首要快捷键

可以找到此特性的有效键码在 Qt::Key and Qt::Modifier 。没有默认的快捷键。

访问函数:

QKeySequence shortcut () const
void setShortcut (const QKeySequence & shortcut )

通知程序信号:

void changed ()

shortcutContext : Qt::ShortcutContext

此特性保持动作快捷方式的上下文

可以找到此特性的有效值在 Qt::ShortcutContext 。默认值为 Qt::WindowShortcut .

访问函数:

Qt::ShortcutContext shortcutContext () const
void setShortcutContext (Qt::ShortcutContext context )

通知程序信号:

void changed ()

statusTip : QString

此特性保持动作的状态提示

状态提示显示在由动作的顶层父级 Widget 提供的所有状态栏上。

默认情况下,此特性包含空字符串。

访问函数:

QString statusTip () const
void setStatusTip (const QString & statusTip )

通知程序信号:

void changed ()

另请参阅 setToolTip () 和 showStatusText ().

text : QString

此特性保持动作的描述性文本

If the action is added to a menu, the menu option will consist of the icon (if there is one), the text, and the shortcut (if there is one). If the text is not explicitly set in the constructor, or by using setText(), the action's description icon text will be used as text. There is no default text.

访问函数:

QString text () const
void setText (const QString & text )

通知程序信号:

void changed ()

另请参阅 iconText .

toolTip : QString

此特性保持动作的工具提示

此文本用于工具提示。若工具提示未指定,使用动作文本。

默认情况下,此特性包含动作的文本。

访问函数:

QString toolTip () const
void setToolTip (const QString & tip )

通知程序信号:

void changed ()

另请参阅 setStatusTip () 和 setShortcut ().

visible : bool

此特性保持动作是否可见 (如在菜单和工具栏中)

visible is true the action can be seen (e.g. in menus and toolbars) and chosen by the user; if visible is false the action cannot be seen or chosen by the user.

Actions which are not visible are not grayed out; they do not appear at all.

默认情况下此特性为 true (动作可见)。

访问函数:

bool isVisible () const
void setVisible ( bool )

通知程序信号:

void changed ()

whatsThis : QString

此特性保持动作的 What's This? 帮助文本

The "What's This?" text is used to provide a brief description of the action. The text may contain rich text. There is no default "What's This?" text.

访问函数:

QString whatsThis () const
void setWhatsThis (const QString & what )

通知程序信号:

void changed ()

另请参阅 QWhatsThis .

成员函数文档编制

QAction:: QAction ( QObject * parent = nullptr)

构造动作采用 parent 。若 parent 是动作组,动作会被自动插入组中。

注意: parent 自变量是可选的,从 Qt 5.7 起。

QAction:: QAction (const QString & text , QObject * parent = nullptr)

构造动作采用一些 text and parent 。若 parent 是动作组,动作会被自动插入组中。

The action uses a stripped version of text (e.g. "&Menu Option..." becomes "Menu Option") as descriptive text for tool buttons. You can override this by setting a specific description with setText (). The same text will be used for tooltips unless you specify a different text using setToolTip ().

QAction:: QAction (const QIcon & icon , const QString & text , QObject * parent = nullptr)

构造动作采用 icon 和一些 text and parent 。若 parent 是动作组,动作会被自动插入组中。

The action uses a stripped version of text (e.g. "&Menu Option..." becomes "Menu Option") as descriptive text for tool buttons. You can override this by setting a specific description with setText (). The same text will be used for tooltips unless you specify a different text using setToolTip ().

QAction:: ~QAction ()

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

QActionGroup *QAction:: actionGroup () const

返回此动作的动作组。返回 0 若没有动作组管理此动作。

另请参阅 QActionGroup and QAction::setActionGroup ().

void QAction:: activate ( ActionEvent event )

发送相关信号为 ActionEvent event .

基于动作的 Widget 使用此 API 以促使 QAction 发射信号及发射它们自己。

QList < QGraphicsWidget *> QAction:: associatedGraphicsWidgets () const

返回已添加此动作的小部件列表。

该函数在 Qt 4.5 引入。

另请参阅 QWidget::addAction () 和 associatedWidgets ().

QList < QWidget *> QAction:: associatedWidgets () const

返回已添加此动作的小部件列表。

该函数在 Qt 4.2 引入。

另请参阅 QWidget::addAction () 和 associatedGraphicsWidgets ().

[signal] void QAction:: changed ()

This signal is emitted when an action has changed. If you are only interested in actions in a given widget, you can watch for QWidget::actionEvent () sent with an QEvent::ActionChanged .

注意: 通知程序信号对于特性 autoRepeat . Notifier signal for property checkable . Notifier signal for property enabled . Notifier signal for property font . Notifier signal for property icon . Notifier signal for property iconText . Notifier signal for property iconVisibleInMenu . Notifier signal for property menuRole . Notifier signal for property shortcut . Notifier signal for property shortcutContext . Notifier signal for property statusTip . Notifier signal for property text . Notifier signal for property toolTip . Notifier signal for property visible . Notifier signal for property whatsThis .

另请参阅 QWidget::actionEvent ().

QVariant QAction:: data () const

返回用户设置数据在 QAction::setData .

另请参阅 setData ().

[virtual protected] bool QAction:: event ( QEvent * e )

重实现自 QObject::event ().

[slot] void QAction:: hover ()

这是调用 activate(Hover) 的方便槽。

[signal] void QAction:: hovered ()

此信号被发射当用户突显动作时;例如:当用户暂停光标在菜单选项、工具栏按钮上或按下动作的快捷键组合时。

另请参阅 QAction::activate ().

bool QAction:: isSeparator () const

返回 true 若此动作是分隔符动作;否则它返回 false .

另请参阅 QAction::setSeparator ().

返回由此动作包含的菜单。包含菜单的动作可以被用于创建带子菜单的菜单项,或被插入工具栏中以创建带弹出菜单的按钮。

另请参阅 setMenu () 和 QMenu::addAction ().

QWidget *QAction:: parentWidget () const

返回父级 Widget。

void QAction:: setActionGroup ( QActionGroup * group )

把此动作组设为 group 。动作会被自动添加到组的动作列表。

组内动作相互排斥。

另请参阅 QActionGroup and QAction::actionGroup ().

void QAction:: setData (const QVariant & userData )

把动作的内部数据设为给定 userData .

另请参阅 data ().

[slot] void QAction:: setDisabled ( bool b )

这是方便函数对于 enabled property, that is useful for signals--slots connections. If b is true the action is disabled; otherwise it is enabled.

void QAction:: setMenu ( QMenu * menu )

把由此动作包含的菜单设为指定 menu .

另请参阅 menu ().

void QAction:: setSeparator ( bool b )

b 为 true,则此动作将被认为是分隔符。

如何表示分隔符从属插入其的 Widget。在大多数情况下,文本、子菜单及图标会被分隔符动作忽略。

另请参阅 QAction::isSeparator ().

void QAction:: setShortcuts (const QList < QKeySequence > & shortcuts )

shortcuts 作为触发动作的快捷方式列表。列表的第一元素是首要快捷方式。

该函数在 Qt 4.2 引入。

另请参阅 shortcuts () 和 shortcut .

void QAction:: setShortcuts ( QKeySequence::StandardKey key )

设置从属平台的快捷方式列表基于 key . The result of calling this function will depend on the currently running platform. Note that more than one shortcut can assigned by this action. If only the primary shortcut is required, use setShortcut 代替。

该函数在 Qt 4.2 引入。

另请参阅 QKeySequence::keyBindings ().

QKeySequence QAction:: shortcut () const

返回首要快捷键。

注意: Getter 函数对于特性 shortcut .

另请参阅 setShortcuts ().

QList < QKeySequence > QAction:: shortcuts () const

返回快捷键列表,采用首要快捷键作为列表的第一元素。

该函数在 Qt 4.2 引入。

另请参阅 setShortcuts ().

bool QAction:: showStatusText ( QWidget * widget = Q_NULLPTR)

更新相关状态栏为 widget 指定通过发送 QStatusTipEvent 到其父级 Widget。返回 true 若事件被发送;否则返回 false .

若指定 null 小部件,则事件被发送给动作的父级。

另请参阅 statusTip .

[slot] void QAction:: toggle ()

这是方便函数对于 checked 特性。连接到它以将复选状态更改为其的相反状态。

[signal] void QAction:: toggled ( bool checked )

此信号被发射每当可复选动作改变其 isChecked () 状态。这可能是用户交互结果,或因为 setChecked () 被调用。

checked 为 true 若动作被复选,或 false 若动作被取消复选。

注意: 通知程序信号对于特性 checked .

另请参阅 QAction::activate (), QAction::triggered (),和 checked .

[slot] void QAction:: trigger ()

这是调用 activate(Trigger) 的方便槽。

[signal] void QAction:: triggered ( bool checked = false)

此信号被发射当动作被激活由用户;例如:当用户点击菜单选项、工具栏按钮或按下动作的快捷键组合时,或当 trigger () 被调用。显而易见,它是 not 被发射当 setChecked () 或 toggle () 被调用。

若动作是可复选的, checked 为 true 若动作被复选,或 false 若动作被取消复选。

另请参阅 QAction::activate (), QAction::toggled (),和 checked .