IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QAction Class

The https:// class provides an abstraction for user commands that can be added to different user interface components.

This class was introduced in Qt 6.0.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QAction Class▲

  • Header: QAction

  • Since: Qt 6.0

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Gui)

    target_link_libraries(mytarget PRIVATE Qt6::Gui)

  • qmake: QT += gui

  • Inherits: https://

  • Inherited By: https://

Detailed Description▲

In applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. Since the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action.

Actions can be added to user interface elements such as menus and toolbars, and will automatically keep the UI in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked.

A https:// may contain an icon, descriptive text, icon text, a keyboard shortcut, status text, "What's This?" text, and a tooltip. All properties can be set independently with https://(), https://(), https://(), https://(), https://(), https://(), and https://(). Icon and text, as the two most important properties, can also be set in the constructor. It's possible to set an individual font with https://(), which e.g. menus respect when displaying the action as a menu item.

We recommend that actions are created as children of the window they are used in. In most cases actions will be children of the application's main window.

QAction in widget applications▲

Once a https:// has been created, it should be added to the relevant menu and toolbar, then connected to the slot which will perform the action.

Actions are added to widgets using https://() or https://(). Note that an action must be added to a widget before it can be used. This is also true when the shortcut should be global (i.e., https:// as https://).

Actions can be created as independent objects. But they may also be created during the construction of menus. The https:// class contains convenience functions for creating actions suitable for use as menu items.

See Also▲

See also https://, https://

Member Type Documentation▲

 

enum QAction::ActionEvent▲

This enum type is used when calling https://()

Constant

Value

Description

https://::Trigger

0

this will cause the https://() signal to be emitted.

https://::Hover

1

this will cause the https://() signal to be emitted.

enum QAction::MenuRole▲

Constant

Value

Description

https://::NoRole

0

This action should not be put into the application menu

https://::TextHeuristicRole

1

This action should be put in the application menu based on the action's text as described in the https:// documentation.

https://::ApplicationSpecificRole

2

This action should be put in the application menu with an application specific role

https://::AboutQtRole

3

This action handles the "About Qt" menu item.

https://::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 https://).

https://::PreferencesRole

5

This action should be placed where the "Preferences..." menu item is in the application menu.

https://::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

https://::LowPriority

0

The action should not be prioritized in the user interface.

https://::NormalPriority

128

 

https://::HighPriority

256

The action should be prioritized in the user interface.

See Also▲

See also https://

Property Documentation▲

 

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.

Access functions:

  • bool autoRepeat() const

  • void setAutoRepeat(bool)

Notifier signal:

  • void https://()

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 https:// with the QActionGroup::exclusive property set to true.

Access functions:

  • bool isCheckable() const

  • void setCheckable(bool)

Notifier signal:

  • void checkableChanged(bool checkable)

See Also▲

See also https://()

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

The notifier signal for this property is https://(). As toggling a https:// changes its state, it will also emit a https://() signal.

Access functions:

  • bool isChecked() const

  • void setChecked(bool)

Notifier signal:

  • void https://(bool checked)

See Also▲

See also https://, https://()

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 https:// property is set.

An action will be disabled when all widgets to which it is added (with https://()) 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).

Access functions:

  • bool isEnabled() const

  • void setEnabled(bool)

  • void resetEnabled()

Notifier signal:

  • void enabledChanged(bool enabled)

See Also▲

See also https://

font : QFont▲

This property holds the action's font

The font property is used to render the text set on the https://. The font 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.

Access functions:

  • font() const

  • void setFont(const &font)

Notifier signal:

  • void https://()

See Also▲

See also https://()

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 (https://()) is passed into this function, the icon of the action is cleared.

Access functions:

  • icon() const

  • void setIcon(const &icon)

Notifier signal:

  • void https://()

iconText : QString▲

This property holds the action's descriptive icon text

If https:// 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 https://() or https://(), and will also be used in toolbar buttons if no icon has been defined using https://().

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.

Access functions:

  • iconText() const

  • void setIconText(const &text)

Notifier signal:

  • void https://()

See Also▲

See also https://(), https://()

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 https:// attribute is set for the application. Explicitly settings this property overrides the presence (or absence) of the attribute.

For example:

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

Access functions:

  • bool isIconVisibleInMenu() const

  • void setIconVisibleInMenu(bool visible)

Notifier signal:

  • void https://()

See Also▲

See also https://, QCoreApplication::setAttribute()

menuRole : MenuRole▲

This indicates what role the action serves in the application menu on macOS. By default all actions have the https://, which means that the action is added based on its text (see https:// 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).

Access functions:

  • menuRole() const

  • void setMenuRole( menuRole)

Notifier signal:

  • void https://()

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 https:// mode set, then actions with https:// will not show the text labels.

Access functions:

  • priority() const

  • void setPriority( priority)

Notifier signal:

  • void https://()

shortcut : QKeySequence▲

This property holds the action's primary shortcut key

Valid keycodes for this property can be found in https:// and https://. There is no default shortcut key.

Access functions:

  • https://() const

  • void https://(const &shortcut)

Notifier signal:

  • void https://()

shortcutContext : Qt::ShortcutContext▲

This property holds the context for the action's shortcut

Valid values for this property can be found in https://. The default value is https://.

Access functions:

  • shortcutContext() const

  • void setShortcutContext( context)

Notifier signal:

  • void https://()

shortcutVisibleInContextMenu : bool▲

This property holds whether or not an action should show a shortcut in a context menu

In some applications, it may make sense to have actions with shortcuts in context menus. If true, the shortcut (if valid) is shown when the action is shown via a context menu, when it is false, it is not shown.

The default is to follow whether the https:// attribute is set for the application. Explicitly setting this property overrides the attribute.

Access functions:

  • bool isShortcutVisibleInContextMenu() const

  • void setShortcutVisibleInContextMenu(bool show)

Notifier signal:

  • void https://()

See Also▲

See also https://, QCoreApplication::setAttribute()

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.

Access functions:

  • statusTip() const

  • void setStatusTip(const &statusTip)

Notifier signal:

  • void https://()

See Also▲

See also https://(), https://()

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.

Certain UI elements, such as menus or buttons, can use '&' in front of a character to automatically create a mnemonic (a shortcut) for that character. For example, "&File" for a menu will create the shortcut Alt+F, which will open the File menu. "E&xit" will create the shortcut Alt+X for a button, or in a menu allow navigating to the menu item by pressing "x". (use '&&' to display an actual ampersand). The widget might consume and perform an action on a given shortcut.

Access functions:

  • text() const

  • void setText(const &text)

Notifier signal:

  • void https://()

See Also▲

See also https://

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.

Access functions:

  • toolTip() const

  • void setToolTip(const &tip)

Notifier signal:

  • void https://()

See Also▲

See also https://(), https://()

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

Access functions:

  • bool isVisible() const

  • void setVisible(bool)

Notifier signal:

  • void visibleChanged()

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.

Access functions:

  • whatsThis() const

  • void setWhatsThis(const &what)

Notifier signal:

  • void https://()

See Also▲

See also https://

Member Function Documentation▲

 

[explicit] 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.

The parent argument is optional since Qt 5.7.

[explicit] 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.

A stripped version of text (for example, "&Menu Option..." becomes "Menu Option") will be used for tooltips and icon text unless you specify a different text using https://() or https://(), respectively.

See Also▲

See also https://

[explicit] 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.

A stripped version of text (for example, "&Menu Option..." becomes "Menu Option") will be used for tooltips and icon text unless you specify a different text using https://() or https://(), respectively.

See Also▲

See also https://, https://

[virtual] 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 nullptr will be returned.

See Also▲

See also https://, https://()

void QAction::activate(QAction::ActionEvent event)▲

Sends the relevant signals for https:// event.

Action-based widgets use this API to cause the https:// to emit signals as well as emitting their own.

[since 6.0] QList<QObject *> QAction::associatedObjects() const▲

Returns a list of objects this action has been added to.

This function was introduced in Qt 6.0.

See Also▲

See also https://(), https://()

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 https://() sent with an https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

Notifier signal for property https://.

See Also▲

See also https://()

QVariant QAction::data() const▲

Returns the user data as set in https://.

See Also▲

See also https://()

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

Reimplements: https://.

void QAction::hover()▲

This is a convenience slot that calls activate(Hover).

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.

See Also▲

See also https://()

bool QAction::isSeparator() const▲

Returns true if this action is a separator action; otherwise it returns false.

See Also▲

See also https://()

QMenu *QAction::menu() const▲

In widget applications, actions that contain menus can be used to create menu items with submenus, or inserted into toolbars to create buttons with popup menus.

See Also▲

See also https://(), https://(), https://()

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.

See Also▲

See also https://, https://()

void QAction::setData(const QVariant &data)▲

Sets the action's internal data to the given data.

See Also▲

See also https://()

void QAction::setDisabled(bool b)▲

This is a convenience function for the https:// 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.

See Also▲

See also https://()

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.

See Also▲

See also https://()

void QAction::setShortcut(const QKeySequence &shortcut)▲

Sets shortcut as the sole shortcut that triggers the action.

Setter function for property https://.

See Also▲

See also https://, https://()

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.

See Also▲

See also https://(), https://, https://()

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 https:// instead.

See Also▲

See also https://()

QKeySequence QAction::shortcut() const▲

Returns the primary shortcut.

Getter function for property https://.

See Also▲

See also https://()

QList<QKeySequence> QAction::shortcuts() const▲

Returns the list of shortcuts, with the primary shortcut as the first element of the list.

See Also▲

See also https://()

bool QAction::showStatusText(QObject *object = nullptr)▲

Updates the relevant status bar for the UI represented by object by sending a https://. 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.

See Also▲

See also https://

void QAction::toggle()▲

This is a convenience function for the https:// property. Connect to it to change the checked state to its opposite state.

void QAction::toggled(bool checked)▲

This signal is emitted whenever a checkable action changes its https://() status. This can be the result of a user interaction, or because https://() was called. As https://() changes the https://, it emits https://() in addition to toggled().

checked is true if the action is checked, or false if the action is unchecked.

Notifier signal for property https://.

See Also▲

See also https://(), https://(), https://

void QAction::trigger()▲

This is a convenience slot that calls activate(Trigger).

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 https://() was called. Notably, it is not emitted when https://() or https://() is called.

If the action is checkable, checked is true if the action is checked, or false if the action is unchecked.

See Also▲

See also https://(), https://(), https://

Obsolete Members for QAction▲

The following members of class https:// are deprecated. We strongly advise against using them in new code.

Obsolete Member Function Documentation▲

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

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use https://() with https://() instead.

Returns a list of graphics widgets this action has been added to.

See Also▲

See also https://(), https://(), https://()

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

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use https://() with https://() instead.

Returns a list of widgets this action has been added to.

See Also▲

See also https://(), https://(), https://()

QWidget *QAction::parentWidget() const▲

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use https://() with https://() instead.

Returns the parent widget.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.