KActionCollection Class

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

Header: #include <KActionCollection>

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
QAction *addAction(const QString &name, QAction *action)
QAction *addAction(KStandardAction::StandardAction actionType, const QObject *receiver = nullptr, const char *member = nullptr)
QAction *addAction(const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)
QAction *addAction(KStandardAction::StandardAction actionType, const QString &name, const QObject *receiver = nullptr, const char *member = nullptr)
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)
void changed()
void inserted(QAction *action)

Static Public Members

const QList<KActionCollection *> &allCollections()
QKeySequence defaultShortcut(QAction *action)
QList<QKeySequence> defaultShortcuts(QAction *action)
bool isShortcutsConfigurable(QAction *action)
void setDefaultShortcut(QAction *action, const QKeySequence &shortcut)
void setDefaultShortcuts(QAction *action, const QList<QKeySequence> &shortcuts)
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.

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 the action with the given \p 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 e.g.

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

after having called setupGUI() or createGUI().

@param name Name of the QAction @return A pointer to the QAction in the collection which 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

Return the QAction* at position @p 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 @p action was hovered.

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

Indicates that @p action was triggered

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

Returns the list of QActions which 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() which belong to this action collection.

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

@param name The name by which the action be retrieved again from the collection. @param action The action to add. @return 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.

@param actionType The standard action type of the action to create. @param receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired. @param member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no connection is desired. @return new action of the given type ActionType.

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.

@param name The name by which the action be retrieved again from the collection. @param receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired. @param member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no connection is desired. @return new action of the given type ActionType.

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.

@param actionType The standard action type of the action to create. @param name The name by which the action be retrieved again from the collection. @param receiver The QObject to connect the triggered(bool) signal to. Leave nullptr if no connection is desired. @param member The SLOT to connect the triggered(bool) signal to. Leave nullptr if no connection is desired. @return new action of the given type ActionType.

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

@param actions the list of the actions to add.

@see addAction() @since 5.0

void KActionCollection::addAssociatedWidget(QWidget *widget)

Associate all actions in this collection to the given @p 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 @p 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] void KActionCollection::changed()

Emitted when an action has been inserted into, or removed from, this action collection. @since 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 ( @c true ), or specific to the application ( @c false ).

Note: Getter function for property configIsGlobal.

int KActionCollection::count() const

Returns the number of actions in the collection.

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

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

Get the default primary shortcut for the given action.

@param action the action for which the default primary shortcut should be returned. @return the default primary shortcut of the given action @since 5.0

See also setDefaultShortcut().

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

Get the default shortcuts for the given action.

@param action the action for which the default shortcuts should be returned. @return the default shortcuts of the given action @since 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 @p config.

config Config object to save to writeDefaults set to true to write settings which are already at defaults.

This function was introduced in 4.1.

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

Import from @p config all configurable global key associations.

config Config object to read from

This function was introduced in 4.1.

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

Indicates that @p action was inserted into this action collection.

bool KActionCollection::isEmpty() const

Returns whether the action collection is empty or not.

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

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

@param action the action for the hint should be verified. @since 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 @p config.

If @p 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. @param action The action to remove.

void KActionCollection::removeAssociatedWidget(QWidget *widget)

Remove an association between all actions in this collection and the given @p 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 display name associated with this action collection. (e.g. for the toolbar editor) KXMLGUIClient::setComponentName takes care of calling this.

See also componentDisplayName().

void KActionCollection::setComponentName(const QString &componentName)

Set the @p componentName associated with this action collection.

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

componentData 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 ( @c true ), or specific to the application ( @c false ).

Note: Setter function for property configIsGlobal.

void KActionCollection::setConfigGroup(const QString &group)

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

Note: Setter function for property configGroup.

See also configGroup().

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

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

@param action the action for which the default shortcut should be set. @param shortcut the shortcut to use for the given action in its specified shortcutContext() @since 5.0

See also defaultShortcut().

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

Set the default shortcuts for the given action. Since 5.2, this also calls action->setShortcuts(shortcuts), i.e. the default shortcut is made active initially.

@param action the action for which the default shortcut should be set. @param shortcuts the shortcuts to use for the given action in its specified shortcutContext() @since 5.0

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

See also defaultShortcuts().

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

Indicate whether the user may configure the action's shortcuts.

@param action the action for the hint should be verified. @param configurable set to true if the shortcuts of the given action may be configured by the user, otherwise false. @since 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.

@param action the action to remove.

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

Write the current configurable key associations to @p config. What the function does if @p 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: @p oneAction and @p writeDefaults have no meaning for the kxmlgui configuration file.

config Config object to save to, or null (see above) 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.