QActionGroup Class

The QActionGroup class groups actions together. More...

Header: #include <QActionGroup>
qmake: QT += widgets
Inherits: QObject

Public Types

enum class ExclusionPolicy { None, Exclusive, ExclusiveOptional }

Properties

Public Functions

QActionGroup::ExclusionPolicy exclusionPolicy() const
bool isEnabled() const
bool isVisible() const

Public Slots

void setEnabled(bool)
void setExclusionPolicy(QActionGroup::ExclusionPolicy policy)
void setVisible(bool)

Detailed Description

In some situations it is useful to group QAction objects together. For example, if you have a Left Align action, a Right Align action, a Justify action, and a Center action, only one of these actions should be active at any one time. One simple way of achieving this is to group the actions together in an action group.

Here's a example (from the Menus example):

     alignmentGroup = new QActionGroup(this);
     alignmentGroup->addAction(leftAlignAct);
     alignmentGroup->addAction(rightAlignAct);
     alignmentGroup->addAction(justifyAct);
     alignmentGroup->addAction(centerAct);
     leftAlignAct->setChecked(true);

Here we create a new action group. Since the action group is exclusive by default, only one of the actions in the group is checked at any one time.

Alignment options in a QMenu

A QActionGroup emits an triggered() signal when one of its actions is chosen. Each action in an action group emits its triggered() signal as usual.

As stated above, an action group is exclusive by default; it ensures that at most only one checkable action is active at any one time. If you want to group checkable actions without making them exclusive, you can turn off exclusiveness by calling setExclusive(false).

By default the active action of an exclusive group cannot be unchecked. In some cases it may be useful to allow unchecking all the actions, you can allow this by calling setExclusionPolicy(QActionGroup::ExclusionPolicy::ExclusiveOptional).

Actions can be added to an action group using addAction(), but it is usually more convenient to specify a group when creating actions; this ensures that actions are automatically created with a parent. Actions can be visually separated from each other by adding a separator action to the group; create an action and use QAction's setSeparator() function to make it considered a separator. Action groups are added to widgets with the QWidget::addActions() function.

See also QAction.

Member Type Documentation

enum class QActionGroup::ExclusionPolicy

This enum specifies the different policies that can be used to control how the group performs exclusive checking on checkable actions.

ConstantValueDescription
QActionGroup::ExclusionPolicy::None0The actions in the group can be checked independently of each other.
QActionGroup::ExclusionPolicy::Exclusive1Exactly one action can be checked at any one time. This is the default policy.
QActionGroup::ExclusionPolicy::ExclusiveOptional2At most one action can be checked at any one time. The actions can also be all unchecked.

This enum was introduced or modified in Qt 5.14.

See also exclusionPolicy.

Property Documentation

enabled : bool

This property holds whether the action group is enabled

Each action in the group will be enabled or disabled unless it has been explicitly disabled.

Access functions:

bool isEnabled() const
void setEnabled(bool)

See also QAction::setEnabled().

exclusionPolicy : QActionGroup::ExclusionPolicy

This property holds the group exclusive checking policy

If exclusionPolicy is set to Exclusive, only one checkable action in the action group can ever be active at any time. If the user chooses another checkable action in the group, the one they chose becomes active and the one that was active becomes inactive. If exclusionPolicy is set to ExclusionOptional the group is exclusive but the active checkable action in the group can be unchecked leaving the group with no actions checked.

This property was introduced in Qt 5.14.

Access functions:

QActionGroup::ExclusionPolicy exclusionPolicy() const
void setExclusionPolicy(QActionGroup::ExclusionPolicy policy)

See also QAction::checkable.

visible : bool

This property holds whether the action group is visible

Each action in the action group will match the visible state of this group unless it has been explicitly hidden.

Access functions:

bool isVisible() const
void setVisible(bool)

See also QAction::setEnabled().