Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Management of QActions (applicationwide)

[Solved] Management of QActions (applicationwide)

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 956 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thEClaw
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on last edited by
      #2

      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! :)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved