KActionCollection Class

A container for a set of QAction objects. More...

Header: #include <KActionCollection>
CMake: find_package(KF6 REQUIRED COMPONENTS XmlGui)
target_link_libraries(mytarget PRIVATE KF6::XmlGui)
Inherits: QObject

Properties

Public Functions

KActionCollection(QObject *parent, const QString &cName = QString())
virtual ~KActionCollection() override
QAction *action(const QString &name) const
QAction *action(int index) const
const QList<QActionGroup *> actionGroups() const
QList<QAction *> actions() const
const QList<QAction *> actionsWithoutGroup() const
ActionType *add(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)
(since 5.28) ActionType *add(const QString &name, const Receiver *receiver, Func slot)
QAction *addAction(const QString &name, QAction *action)
QAction *addAction(KStandardAction::StandardAction actionType, const QObject *receiver = nullptr, const char *member = nullptr)
(since 6.3) QAction *addAction(KStandardActions::StandardAction actionType, const Receiver *receiver, Func slot)
QAction *addAction(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)
(since 5.28) QAction *addAction(const QString &name, const Receiver *receiver, Func slot)
QAction *addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)
(since 5.80) QAction *addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot)
(since 6.3) QAction *addAction(KStandardActions::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot)
(since 5.0) void addActions(const QList<QAction *> &actions)
void addAssociatedWidget(QWidget *widget)
void associateWidget(QWidget *widget) const
QList<QWidget *> associatedWidgets() const
void clear()
void clearAssociatedWidgets()
QString componentDisplayName() const
QString componentName() const
QString configGroup() const
bool configIsGlobal() const
int count() const
(since 4.1) void exportGlobalShortcuts(KConfigGroup *config, bool writeDefaults = false) const
(since 4.1) void importGlobalShortcuts(KConfigGroup *config)
bool isEmpty() const
const KXMLGUIClient *parentGUIClient() const
void readSettings(KConfigGroup *config = nullptr)
void removeAction(QAction *action)
void removeAssociatedWidget(QWidget *widget)
void setComponentDisplayName(const QString &displayName)
void setComponentName(const QString &componentName)
void setConfigGlobal(bool global)
void setConfigGroup(const QString &group)
QAction *takeAction(QAction *action)
void writeSettings(KConfigGroup *config = nullptr, bool writeDefaults = false, QAction *oneAction = nullptr) const

Signals

void actionHovered(QAction *action)
void actionTriggered(QAction *action)
(since 5.66) void changed()
void inserted(QAction *action)

Static Public Members

const QList<KActionCollection *> &allCollections()
(since 5.0) QKeySequence defaultShortcut(QAction *action)
(since 5.0) QList<QKeySequence> defaultShortcuts(QAction *action)
(since 5.0) bool isShortcutsConfigurable(QAction *action)
(since 5.0) void setDefaultShortcut(QAction *action, const QKeySequence &shortcut)
(since 5.0) void setDefaultShortcuts(QAction *action, const QList<QKeySequence> &shortcuts)
(since 5.0) void setShortcutsConfigurable(QAction *action, bool configurable)

Detailed Description

KActionCollection manages a set of QAction objects. It allows them to be grouped for organized presentation of configuration to the user, saving + loading of configuration, and optionally for automatic plugging into specified widget(s).

Additionally, KActionCollection provides several convenience functions for locating named actions, and actions grouped by QActionGroup.

Note: If you create your own action collection and need to assign shortcuts to the actions within, you have to call associateWidget() or addAssociatedWidget() to have them working.

Property Documentation

configGroup : QString

Access functions:

QString configGroup() const
void setConfigGroup(const QString &group)

configIsGlobal : bool

Access functions:

bool configIsGlobal() const
void setConfigGlobal(bool global)

Member Function Documentation

[explicit] KActionCollection::KActionCollection(QObject *parent, const QString &cName = QString())

Constructor.

Allows specification of a component name other than the default application name, where needed (remember to call setComponentDisplayName() too).

[override virtual noexcept] KActionCollection::~KActionCollection()

Destructor.

[invokable] QAction *KActionCollection::action(const QString &name) const

Get an action with the given name from the action collection.

This won't return the action for the menus defined using a "<Menu>" tag in XMLGUI files (e.g. "<Menu name="menuId">" in "applicationNameui.rc").

To access menu actions defined like this, use for example...

qobject_cast<QMenu *>(guiFactory()->container("menuId", this));

...after having called setupGUI() or createGUI().

Returns a pointer to the QAction in the collection that matches the parameters or null if nothing matches.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

QAction *KActionCollection::action(int index) const

Returns the QAction* at position index in the action collection.

This is equivalent to actions().value(index).

const QList<QActionGroup *> KActionCollection::actionGroups() const

Returns the list of all QActionGroups associated with actions in this action collection.

[signal] void KActionCollection::actionHovered(QAction *action)

Indicates that action was hovered.

[signal] void KActionCollection::actionTriggered(QAction *action)

Indicates that action was triggered.

QList<QAction *> KActionCollection::actions() const

Returns the list of QActions that belong to this action collection.

The list is guaranteed to be in the same order the action were put into the collection.

const QList<QAction *> KActionCollection::actionsWithoutGroup() const

Returns the list of QActions without an QAction::actionGroup() that belong to this action collection.

template <typename ActionType> ActionType *KActionCollection::add(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)

Creates a new action under the given name, adds it to the collection, and connects the action's triggered(bool) signal to the specified receiver/member.

The receiver slot may accept either a bool or no parameters at all (i.e. slotTriggered(bool) or slotTriggered() ). The type of the action is specified by the template parameter ActionType.

Note: KDE prior to 4.2 connected the triggered() signal instead of the triggered(bool) signal.

The KActionCollection takes ownership of the action object.

name The internal name of the action (e.g. "file-open").

receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

Returns new action of the given type ActionType.

See also addAction().

[since 5.28] template <typename ActionType, typename Receiver, typename Func> ActionType *KActionCollection::add(const QString &name, const Receiver *receiver, Func slot)

This is the same as add(const QString &name, const QObject *receiver, const char *member) but using new style connect syntax.

name The internal name of the action (e.g. "file-open").

receiver The QObject to connect the triggered(bool) signal to.

slot The slot or lambda to connect the triggered(bool) signal to.

Returns new action of the given type ActionType.

This function was introduced in 5.28.

See also add(const QString &, const QObject *, const char *).

[invokable] QAction *KActionCollection::addAction(const QString &name, QAction *action)

Add an action under the given name to the collection.

Inserting an action that was previously inserted under a different name will replace the old entry, i.e. the action will not be available under the old name anymore but only under the new one.

Inserting an action under a name that is already used for another action will replace the other action in the collection (but will not delete it).

If KAuthorized::authorizeAction() reports that the action is not authorized, it will be disabled and hidden.

The ownership of the action object is not transferred. If the action is destroyed it will be removed automatically from the KActionCollection.

name The name by which the action be retrieved again from the collection.

action The action to add.

Returns the same as the action given as parameter. This is just for convenience (chaining calls) and consistency with the other addAction methods, you can also simply ignore the return value.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

QAction *KActionCollection::addAction(KStandardAction::StandardAction actionType, const QObject *receiver = nullptr, const char *member = nullptr)

Creates a new standard action, adds it to the collection and connects the action's triggered(bool) signal to the specified receiver/member. The newly created action is also returned.

Note: Using KStandardAction::OpenRecent will cause a different signal than triggered(bool) to be used, see KStandardAction for more information.

The action can be retrieved later from the collection by its standard name as per KStandardAction::stdName.

The KActionCollection takes ownership of the action object.

actionType The standard action type of the action to create.

receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

Returns new action of the given type ActionType.

[since 6.3] template <typename Receiver, typename Func> QAction *KActionCollection::addAction(KStandardActions::StandardAction actionType, const Receiver *receiver, Func slot)

Creates a new standard action, adds it to the collection and connects the action's triggered(bool) signal to the specified receiver/member. The newly created action is also returned.

Note: Using KStandardAction::OpenRecent will cause a different signal than triggered(bool) to be used, see KStandardAction for more information.

The action can be retrieved later from the collection by its standard name as per KStandardAction::stdName.

The KActionCollection takes ownership of the action object.

actionType The standard action type of the action to create.

receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

slot The slot or lambda to connect the triggered(bool) signal to.

Returns new action of the given type ActionType.

This function was introduced in 6.3.

QAction *KActionCollection::addAction(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)

Creates a new action under the given name to the collection and connects the action's triggered(bool) signal to the specified receiver/member. The newly created action is returned.

Note: KDE prior to 4.2 used the triggered() signal instead of the triggered(bool) signal.

Inserting an action that was previously inserted under a different name will replace the old entry, i.e. the action will not be available under the old name anymore but only under the new one.

Inserting an action under a name that is already used for another action will replace the other action in the collection.

The KActionCollection takes ownership of the action object.

name The name by which the action be retrieved again from the collection.

receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

Returns new action of the given type ActionType.

[since 5.28] template <typename Receiver, typename Func> QAction *KActionCollection::addAction(const QString &name, const Receiver *receiver, Func slot)

This is the same as addAction(const QString &name, const QObject *receiver, const char *member) but using new style connect syntax.

name The internal name of the action (e.g. "file-open").

receiver The QObject to connect the triggered(bool) signal to.

slot The slot or lambda to connect the triggered(bool) signal to.

Returns new action of the given type ActionType.

This function was introduced in 5.28.

See also addAction(const QString &, const QObject *, const char *).

QAction *KActionCollection::addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)

Creates a new standard action, adds to the collection under the given name and connects the action's triggered(bool) signal to the specified receiver/member. The newly created action is also returned.

Note: Using KStandardAction::OpenRecent will cause a different signal than triggered(bool) to be used, see KStandardAction for more information.

The action can be retrieved later from the collection by the specified name.

The KActionCollection takes ownership of the action object.

actionType The standard action type of the action to create.

name The name by which the action be retrieved again from the collection.

receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no connection is desired.

Returns new action of the given type ActionType.

[since 5.80] template <typename Receiver, typename Func> QAction *KActionCollection::addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot)

This is the same as addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver, const char *member) but using new style connect syntax.

actionType The standard action type of the action to create.

name The name by which the action be retrieved again from the collection.

receiver The QObject to connect the triggered(bool) signal to.

slot The slot or lambda to connect the triggered(bool) signal to.

Returns new action of the given type ActionType.

This function was introduced in 5.80.

See also addAction(KStandardAction::StandardAction, const QString &, const QObject *, const char *).

[since 6.3] template <typename Receiver, typename Func> QAction *KActionCollection::addAction(KStandardActions::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot)

This is the same as addAction(KStandardAction::StandardAction actionType, const QString &name, const Receiver *receiver, Func slot) but using KStandardActions from KConfigGui.

actionType The standard action type of the action to create.

name The name by which the action be retrieved again from the collection.

receiver The QObject to connect the triggered(bool) signal to.

slot The slot or lambda to connect the triggered(bool) signal to.

Returns new action of the given type ActionType.

This function was introduced in 6.3.

[since 5.0] void KActionCollection::addActions(const QList<QAction *> &actions)

Adds a list of actions to the collection.

The objectName of the actions is used as their internal name in the collection.

The ownership of the action objects is not transferred. If the action is destroyed it will be removed automatically from the KActionCollection.

Uses addAction(const QString&, QAction*).

This function was introduced in 5.0.

See also addAction().

void KActionCollection::addAssociatedWidget(QWidget *widget)

Associate all actions in this collection to the given widget, including any actions added after this association is made.

This does not change the action's shortcut context, so if you need to have the actions only trigger when the widget has focus, you'll need to set the shortcut context on each action to Qt::WidgetShortcut (or better still, Qt::WidgetWithChildrenShortcut with Qt 4.4+)

[static] const QList<KActionCollection *> &KActionCollection::allCollections()

Access the list of all action collections in existence for this app.

void KActionCollection::associateWidget(QWidget *widget) const

Associate all actions in this collection to the given widget.

Unlike addAssociatedWidget(), this method only adds all current actions in the collection to the given widget. Any action added after this call will not be added to the given widget automatically.

So this is just a shortcut for a foreach loop and a widget->addAction call.

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

Return a list of all associated widgets.

[signal, since 5.66] void KActionCollection::changed()

Emitted when an action has been inserted into, or removed from, this action collection.

This function was introduced in 5.66.

void KActionCollection::clear()

Clears the entire action collection, deleting all actions.

void KActionCollection::clearAssociatedWidgets()

Clear all associated widgets and remove the actions from those widgets.

QString KActionCollection::componentDisplayName() const

The display name for the associated component.

See also setComponentDisplayName().

QString KActionCollection::componentName() const

The component name with which this class is associated.

See also setComponentName().

QString KActionCollection::configGroup() const

Returns the KConfig group with which settings will be loaded and saved.

Note: Getter function for property configGroup.

See also setConfigGroup().

bool KActionCollection::configIsGlobal() const

Returns whether this action collection's configuration should be global to KDE ( true ), or specific to the application ( false ).

Note: Getter function for property configIsGlobal.

int KActionCollection::count() const

The number of actions in the collection.

This is equivalent to actions().count().

[static, since 5.0] QKeySequence KActionCollection::defaultShortcut(QAction *action)

Returns the default primary shortcut for the given action.

This function was introduced in 5.0.

See also setDefaultShortcut().

[static, since 5.0] QList<QKeySequence> KActionCollection::defaultShortcuts(QAction *action)

Returns the default shortcuts for the given action.

This function was introduced in 5.0.

See also setDefaultShortcuts().

[since 4.1] void KActionCollection::exportGlobalShortcuts(KConfigGroup *config, bool writeDefaults = false) const

Export the current configurable global key associations to config.

If writeDefaults is set to true, default settings will also be written.

This function was introduced in 4.1.

[since 4.1] void KActionCollection::importGlobalShortcuts(KConfigGroup *config)

Import all configurable global key associations from config.

This function was introduced in 4.1.

[signal] void KActionCollection::inserted(QAction *action)

Indicates that action was inserted into this action collection.

bool KActionCollection::isEmpty() const

Returns whether the action collection is empty or not.

[static, since 5.0] bool KActionCollection::isShortcutsConfigurable(QAction *action)

Returns true if the given action shortcuts may be configured by the user.

This function was introduced in 5.0.

const KXMLGUIClient *KActionCollection::parentGUIClient() const

The parent KXMLGUIClient, or null if not available.

void KActionCollection::readSettings(KConfigGroup *config = nullptr)

Read all key associations from config.

If config is zero, read all key associations from the application's configuration file KSharedConfig::openConfig(), in the group set by setConfigGroup().

void KActionCollection::removeAction(QAction *action)

Removes an action from the collection and deletes it.

void KActionCollection::removeAssociatedWidget(QWidget *widget)

Remove an association between all actions in this collection and the given widget, i.e. remove those actions from the widget, and stop associating newly added actions as well.

void KActionCollection::setComponentDisplayName(const QString &displayName)

Set the component displayName associated with this action collection.

This can be done, for example, for the toolbar editor.

KXMLGUIClient::setComponentName takes care of calling this.

See also componentDisplayName().

void KActionCollection::setComponentName(const QString &componentName)

Sets the componentName associated with this action collection.

Warning: Don't call this method on a KActionCollection that contains actions. This is not supported.

componentName The name which is to be associated with this action collection, or QString() to indicate the app name. This is used to load/save settings into XML files. KXMLGUIClient::setComponentName takes care of calling this.

See also componentName().

void KActionCollection::setConfigGlobal(bool global)

Set whether this action collection's configuration should be global to KDE ( true ), or specific to the application ( false ).

Note: Setter function for property configIsGlobal.

void KActionCollection::setConfigGroup(const QString &group)

Sets group as the KConfig group with which settings will be loaded and saved.

Note: Setter function for property configGroup.

See also configGroup().

[static, since 5.0] void KActionCollection::setDefaultShortcut(QAction *action, const QKeySequence &shortcut)

Sets the default shortcut for the given action.

Since 5.2, this also calls action->setShortcut(shortcut), i.e. the default shortcut is made active initially.

This function was introduced in 5.0.

See also defaultShortcut().

[static invokable, since 5.0] void KActionCollection::setDefaultShortcuts(QAction *action, const QList<QKeySequence> &shortcuts)

*

Sets the default shortcuts in their specified shortcutContext() * for the given action. * * Since 5.2, this also calls action->setShortcuts(shortcuts), * i.e. the default shortcut is made active initially.

*

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

This function was introduced in 5.0.

See also defaultShortcuts().

[static, since 5.0] void KActionCollection::setShortcutsConfigurable(QAction *action, bool configurable)

Sets whether the action shortcuts may be configurable by the user.

This function was introduced in 5.0.

See also isShortcutsConfigurable().

QAction *KActionCollection::takeAction(QAction *action)

Removes an action from the collection.

The ownership of the action object is not changed.

void KActionCollection::writeSettings(KConfigGroup *config = nullptr, bool writeDefaults = false, QAction *oneAction = nullptr) const

Write the current configurable key associations to config. What the function does if config is zero depends. If this action collection belongs to a KXMLGUIClient the setting are saved to the kxmlgui definition file. If not the settings are written to the applications config file.

Note: oneAction and writeDefaults have no meaning for the kxmlgui configuration file.

config Config object to save to, or null.

writeDefaults set to true to write settings which are already at defaults.

oneAction pass an action here if you just want to save the values for one action, eg. if you know that action is the only one which has changed.