QAction Class
QAction 提供一个插入widget的抽象UI行为. 更多...
头文件: | #include <QAction> |
qmake: | QT += widgets |
基类: | QObject |
派生类: |
公有类型
enum | ActionEvent { Trigger, Hover } |
enum | MenuRole { NoRole, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole, ..., QuitRole } |
enum | Priority { LowPriority, NormalPriority, HighPriority } |
属性
|
- 1 个属性继承自 QObject
公有函数
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 | 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 |
- 32 个公有函数继承自 QObject
公有槽函数
void | hover() |
void | setChecked(bool) |
void | setDisabled(bool b) |
void | setEnabled(bool) |
void | setVisible(bool) |
void | toggle() |
void | trigger() |
- 1 个公有槽函数继承自 QObject
信号
- 2 个信号继承自 QObject
重新实现的受保护函数
virtual bool | event(QEvent *e) |
- 9 个受保护的函数继承自 QObject
其他继承的成员
详细描述
QAction 提供一个插入widget的抽象UI action.
在应用程序中, 多数命令通过菜单, 工具条及键盘快捷键调用. 因为用户希望每个命令以相同方式执行, 所以不管使用什么用户界面, 将不同的命令表示为一个 action 是有用的.
action可以加入菜单和工具栏, 自动保持同步. 如, 在文字处理程序中, 用户点击工具栏按钮(粗体), 菜单(粗体)也会自动选中.
action可以作为独立对象创建, 也可以在构建菜单时创建; QMenu 包含便捷函数创建菜单.
QAction 可以包含图标, 菜单, 快捷键, 状态和工具提示. 其中大部分在构造函数设置. 它们也可以调用 setIcon(), setText(), setIconText(), setShortcut(), setStatusTip(), setWhatsThis(), setToolTip(). 对于菜单, 你可以调用 setFont()单独设置字体.
调用 QWidget::addAction() 或 QGraphicsWidget::addAction()将action加入widget. 注意: action必须在使用前, 加入widget; 快捷键作为全局快捷键 (即., 将 Qt::ShortcutContext设置为Qt::ApplicationShortcut时, 也应如此).
创建 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);
我们建议action作为所在窗口的子对象. 多数情况下, action是应用程序主窗口的子对象.
参见 QMenu, QToolBar, Application Example.
成员类型
enum QAction::ActionEvent
This enum type is used when calling QAction::activate()
Constant | Value | Description |
---|---|---|
QAction::Trigger | 0 | this will cause the QAction::triggered() signal to be emitted. |
QAction::Hover | 1 | this will cause the QAction::hovered() signal to be emitted. |
enum QAction::MenuRole
This enum describes how an action should be moved into the application menu on macOS.
Constant | Value | Description |
---|---|---|
QAction::NoRole | 0 | This action should not be put into the application menu |
QAction::TextHeuristicRole | 1 | This action should be put in the application menu based on the action's text as described in the QMenuBar documentation. |
QAction::ApplicationSpecificRole | 2 | This action should be put in the application menu with an application specific role |
QAction::AboutQtRole | 3 | This action handles the "About Qt" menu item. |
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 - Deployment). |
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
This enum defines priorities for actions in user interface.
Constant | Value | Description |
---|---|---|
QAction::LowPriority | 0 | The action should not be prioritized in the user interface. |
QAction::NormalPriority | 128 | |
QAction::HighPriority | 256 | The action should be prioritized in the user interface. |
This enum was introduced or modified in Qt 4.6.
参见 priority.
属性
autoRepeat : bool
This property holds whether the action can auto repeat
If true, the action will auto repeat when the keyboard shortcut combination is held down, provided that keyboard auto repeat is enabled on the system. The default value is true.
This property was introduced in Qt 4.2.
访问函数:
bool | autoRepeat() const |
void | setAutoRepeat(bool) |
Notifier signal:
void | changed() |
checkable : bool
This property holds whether the action is a checkable action
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 with the QActionGroup::exclusive property set to true.
访问函数:
bool | isCheckable() const |
void | setCheckable(bool) |
Notifier signal:
void | changed() |
参见 QAction::setChecked().
checked : bool
This property holds whether the action is checked.
Only checkable actions can be checked. By default, this is false (the action is unchecked).
访问函数:
bool | isChecked() const |
void | setChecked(bool) |
Notifier signal:
void | toggled(bool checked) |
参见 checkable.
enabled : bool
This property holds whether the action is enabled
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.
By default, this property is true
(actions are enabled).
访问函数:
bool | isEnabled() const |
void | setEnabled(bool) |
Notifier signal:
void | changed() |
参见 text.
font : QFont
This property holds the action's font
The font property is used to render the text set on the QAction. The font will can be considered a hint as it will not be consulted in all cases based upon application and style.
By default, this property contains the application's default font.
访问函数:
QFont | font() const |
void | setFont(const QFont &font) |
Notifier signal:
void | changed() |
参见 QAction::setText() and QStyle.
icon : QIcon
This property holds the action's icon
In toolbars, the icon is used as the tool button icon; in menus, it is displayed to the left of the menu text. There is no default icon.
If a null icon (QIcon::isNull()) is passed into this function, the icon of the action is cleared.
访问函数:
QIcon | icon() const |
void | setIcon(const QIcon &icon) |
Notifier signal:
void | changed() |
iconText : QString
This property holds the action's descriptive icon text
If 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() or setToolTip(), and will also be used in toolbar buttons if no icon has been defined using setIcon().
If the icon text is not explicitly set, the action's normal text will be used for the icon text.
By default, this property contains an empty string.
访问函数:
QString | iconText() const |
void | setIconText(const QString &text) |
Notifier signal:
void | changed() |
参见 setToolTip() and setStatusTip().
iconVisibleInMenu : bool
This property holds whether or not an action should show an icon in a menu
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.
For example:
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.
This property was introduced in Qt 4.4.
访问函数:
bool | isIconVisibleInMenu() const |
void | setIconVisibleInMenu(bool visible) |
Notifier signal:
void | changed() |
参见 QAction::icon and QCoreApplication::setAttribute().
menuRole : MenuRole
This property holds the action's menu role
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).
This property was introduced in Qt 4.2.
访问函数:
MenuRole | menuRole() const |
void | setMenuRole(MenuRole menuRole) |
Notifier signal:
void | changed() |
priority : Priority
This property holds the actions's priority in the user interface.
This property can be set to indicate how the action should be prioritized in the user interface.
For instance, when toolbars have the Qt::ToolButtonTextBesideIcon mode set, then actions with LowPriority will not show the text labels.
This property was introduced in Qt 4.6.
访问函数:
Priority | priority() const |
void | setPriority(Priority priority) |
shortcut : QKeySequence
This property holds the action's primary shortcut key
Valid keycodes for this property can be found in Qt::Key and Qt::Modifier. There is no default shortcut key.
访问函数:
QKeySequence | shortcut() const |
void | setShortcut(const QKeySequence &shortcut) |
Notifier signal:
void | changed() |
shortcutContext : Qt::ShortcutContext
This property holds the context for the action's shortcut
Valid values for this property can be found in Qt::ShortcutContext. The default value is Qt::WindowShortcut.
访问函数:
Qt::ShortcutContext | shortcutContext() const |
void | setShortcutContext(Qt::ShortcutContext context) |
Notifier signal:
void | changed() |
statusTip : QString
This property holds the action's status tip
The status tip is displayed on all status bars provided by the action's top-level parent widget.
By default, this property contains an empty string.
访问函数:
QString | statusTip() const |
void | setStatusTip(const QString &statusTip) |
Notifier signal:
void | changed() |
参见 setToolTip() and showStatusText().
text : QString
This property holds the action's descriptive text
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) |
Notifier signal:
void | changed() |
参见 iconText.
toolTip : QString
This property holds the action's tooltip
This text is used for the tooltip. If no tooltip is specified, the action's text is used.
By default, this property contains the action's text.
访问函数:
QString | toolTip() const |
void | setToolTip(const QString &tip) |
Notifier signal:
void | changed() |
参见 setStatusTip() and setShortcut().
visible : bool
This property holds whether the action can be seen (e.g. in menus and toolbars)
If 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.
By default, this property is true
(actions are visible).
访问函数:
bool | isVisible() const |
void | setVisible(bool) |
Notifier signal:
void | changed() |
whatsThis : QString
This property holds the action's "What's This?" help text
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) |
Notifier signal:
void | changed() |
参见 QWhatsThis.
成员函数
QAction::QAction(QObject *parent = nullptr)
Constructs an action with parent. If parent is an action group the action will be automatically inserted into the group.
QAction::QAction(const QString &text, QObject *parent = nullptr)
Constructs an action with some text and parent. If parent is an action group the action will be automatically inserted into the group.
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)
Constructs an action with an icon and some text and parent. If parent is an action group the action will be automatically inserted into the group.
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()
Destroys the object and frees allocated resources.
QActionGroup *QAction::actionGroup() const
Returns the action group for this action. If no action group manages this action then 0 will be returned.
参见 QActionGroup and QAction::setActionGroup().
void QAction::activate(ActionEvent event)
Sends the relevant signals for ActionEvent event.
Action based widgets use this API to cause the QAction to emit signals as well as emitting their own.
QList<QGraphicsWidget *> QAction::associatedGraphicsWidgets() const
Returns a list of widgets this action has been added to.
This function was introduced in Qt 4.5.
参见 QWidget::addAction() and associatedWidgets().
QList<QWidget *> QAction::associatedWidgets() const
Returns a list of widgets this action has been added to.
This function was introduced in Qt 4.2.
参见 QWidget::addAction() and 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.
Note: Notifier signal for property 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
Returns the user data as set in QAction::setData.
参见 setData().
[virtual protected]
bool QAction::event(QEvent *e)
重新实现 QObject::event().
[slot]
void QAction::hover()
This is a convenience slot that calls activate(Hover).
[signal]
void QAction::hovered()
This signal is emitted when an action is highlighted by the user; for example, when the user pauses with the cursor over a menu option, toolbar button, or presses an action's shortcut key combination.
参见 QAction::activate().
bool QAction::isSeparator() const
Returns true
if this action is a separator action; otherwise it returns false
.
参见 QAction::setSeparator().
QMenu *QAction::menu() const
Returns the menu contained by this action. Actions that contain menus can be used to create menu items with submenus, or inserted into toolbars to create buttons with popup menus.
参见 setMenu() and QMenu::addAction().
QWidget *QAction::parentWidget() const
Returns the parent widget.
void QAction::setActionGroup(QActionGroup *group)
Sets this action group to group. The action will be automatically added to the group's list of actions.
Actions within the group will be mutually exclusive.
参见 QActionGroup and QAction::actionGroup().
void QAction::setData(const QVariant &userData)
Sets the action's internal data to the given userData.
参见 data().
[slot]
void QAction::setDisabled(bool b)
This is a convenience function for the 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)
Sets the menu contained by this action to the specified menu.
参见 menu().
void QAction::setSeparator(bool b)
If b is true then this action will be considered a separator.
How a separator is represented depends on the widget it is inserted into. Under most circumstances the text, submenu, and icon will be ignored for separator actions.
参见 QAction::isSeparator().
void QAction::setShortcuts(const QList<QKeySequence> &shortcuts)
Sets shortcuts as the list of shortcuts that trigger the action. The first element of the list is the primary shortcut.
This function was introduced in Qt 4.2.
void QAction::setShortcuts(QKeySequence::StandardKey key)
Sets a platform dependent list of shortcuts based on the 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 instead.
This function was introduced in Qt 4.2.
参见 QKeySequence::keyBindings().
QKeySequence QAction::shortcut() const
Returns the primary shortcut.
Note: Getter function for property shortcut.
参见 setShortcuts().
QList<QKeySequence> QAction::shortcuts() const
Returns the list of shortcuts, with the primary shortcut as the first element of the list.
This function was introduced in Qt 4.2.
参见 setShortcuts().
bool QAction::showStatusText(QWidget *widget = Q_NULLPTR)
Updates the relevant status bar for the widget specified by sending a QStatusTipEvent to its parent widget. Returns true
if an event was sent; otherwise returns false
.
If a null widget is specified, the event is sent to the action's parent.
参见 statusTip.
[slot]
void QAction::toggle()
This is a convenience function for the checked property. Connect to it to change the checked state to its opposite state.
[signal]
void QAction::toggled(bool checked)
This signal is emitted whenever a checkable action changes its isChecked() status. This can be the result of a user interaction, or because setChecked() was called.
checked is true if the action is checked, or false if the action is unchecked.
Note: Notifier signal for property checked.
参见 QAction::activate(), QAction::triggered(), and checked.
[slot]
void QAction::trigger()
This is a convenience slot that calls activate(Trigger).
[signal]
void QAction::triggered(bool checked = false)
This signal is emitted when an action is activated by the user; for example, when the user clicks a menu option, toolbar button, or presses an action's shortcut key combination, or when trigger() was called. Notably, it is not emitted when setChecked() or toggle() is called.
If the action is checkable, checked is true if the action is checked, or false if the action is unchecked.
参见 QAction::activate(), QAction::toggled(), and checked.