- // do we already have this action under a different name?
- int oldIndex = _actions.indexOf(action);
- if(oldIndex != -1) {
- _actionByName.remove(origName);
- _actions.removeAt(oldIndex);
- }
+QAction *ActionCollection::addAction(const QString &name, QAction *action)
+{
+ if (!action)
+ return action;
+
+ const QString origName = action->objectName();
+ QString indexName = name;
+
+ if (indexName.isEmpty())
+ indexName = action->objectName();
+ else
+ action->setObjectName(indexName);
+ if (indexName.isEmpty())
+ indexName = indexName.sprintf("unnamed-%p", (void *)action);
+
+ // do we already have this action?
+ if (_actionByName.value(indexName, 0) == action)
+ return action;
+ // or maybe another action under this name?
+ if (QAction *oldAction = _actionByName.value(indexName))
+ takeAction(oldAction);
+
+ // do we already have this action under a different name?
+ int oldIndex = _actions.indexOf(action);
+ if (oldIndex != -1) {
+ _actionByName.remove(origName);
+ _actions.removeAt(oldIndex);
+ }