[Solved] Management of QActions (applicationwide)
-
This is a somewhat "high-level" problem, not necessarily will there be code involved. (This hurts my grammar senses. Sorry if it's wrong!)
I have a main window (the one created before the eventloop starts) and I added a couple of actions to it. I need these actions at other places in the application, some of them only distantly related. So now I am looking for a pretty and efficient way of storing these actions and making them available.
Until now I used:
@mainwindow->findChildren<QAction *>()@and then searched the list, or
@mainwindow->findChild<QAction*>("name of the action-object")@These actions seem quite expensive, since the application is not exactly small (have seen several thousand actions to go through with the above methods). So I was thinking about either making the actions globally available (maybe via an accessible QHash<QString, QAction *>), or use some kind of static "helper class", that wraps the QHash. But both of these ideas don't really please me.
I would be happy for some input on this, and hope to find a neat, extensible solution where I can easily add QActions as the application is developed further.Have a nice day, thanks in advance. :)
-
Well, since nobody offered a better idea, I went with this:
@class ActionManager
{
public:
static ActionManager &getSingleton();QAction *getAction(const QString &a);
void init(QWidget *parent);//creates all actions and parents them to parent
private:
ActionManager() : initialized(false) {}
ActionManager(ActionManager const &other);
void operator=(ActionManager const &other);QHash<QString, QAction *> actions;
void setIcon(QAction *a, const QString &path, QString altPath = QString());
void setProperties(QAction *a, const QString &title, bool doNotIncludeForChildren = false, bool draggable = true);bool initialized;
};@It is not the prettiest thing ever, especially since the init() function grows rapidly. But by using lambda expressions the rest stays quite simple.
Now I can access an action from anywhere in the application via
@ActionManager::getSingleton().getAction("Name of Action");@I don't know if that is actually faster than using findChildren(), but at least I feel kind of in control of that now. Still, if anybody had some input on how to improve this concept, I'd be glad to listen! :)