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. Storing QAction and slot in a struct?
Qt 6.11 is out! See what's new in the release blog

Storing QAction and slot in a struct?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k Views 2 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.
  • L Offline
    L Offline
    lansing
    wrote on last edited by
    #1

    I want to create many QAction for my QMenu. And since QAction need to be attached with a signal/slot connection each, I want to first store the QAction and their slots in an array and then populate them in a for loop later.

    I use a struct to package QAction and slot into one object, then put each into an array

    // Actions are first declared in the header
    QAction * newAct;
    QAction * openAct;
    
    struct ActionSlotPack{
       QAction * pAction;
       // void * connectionSlot?
    }
    
    // storing them into an array
    ActionSlotPack actionSlots[] =
       {
           {newAct, slotNew()},
           {openAct, slotOpen()}
       }
    
    // populating the connection for actions
    for(ActionSlotPack & item : actionSlots)
    {
       connect(item.pAction, &QAction::triggered, this, &item.connectionSlot);
    }
    

    I don't know the syntax to store the slot in the struct and output them in a for loop.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Is all of them slotNew, slotOpen member of same class?

      Basically you want to store a function pointer to a member
      They all have same arguments i assume ?

      try with std::function
      https://stackoverflow.com/questions/20353210/usage-and-syntax-of-stdfunction

      or if you are a bit lazy, the SLOT function might work as its simply a char *
      but you are using the new connection syntax so better to try with real member pointer.

      Do note that this

      ActionSlotPack actionSlots[] =
         {
             {newAct, slotNew()},
             {openAct, slotOpen()}
         }
      

      cannot be global as the slots belongs to an object and we need an instance to grab pointer to it.
      like

      ActionSlotPack actionSlots[] =
         {
             {newAct, object->slotNew()},
             {openAct, object->slotOpen()}
         }
      
      L 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Is all of them slotNew, slotOpen member of same class?

        Basically you want to store a function pointer to a member
        They all have same arguments i assume ?

        try with std::function
        https://stackoverflow.com/questions/20353210/usage-and-syntax-of-stdfunction

        or if you are a bit lazy, the SLOT function might work as its simply a char *
        but you are using the new connection syntax so better to try with real member pointer.

        Do note that this

        ActionSlotPack actionSlots[] =
           {
               {newAct, slotNew()},
               {openAct, slotOpen()}
           }
        

        cannot be global as the slots belongs to an object and we need an instance to grab pointer to it.
        like

        ActionSlotPack actionSlots[] =
           {
               {newAct, object->slotNew()},
               {openAct, object->slotOpen()}
           }
        
        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @mrjj

        Hi the slots can be a boolean or a void with no arguments.

        mrjjM 1 Reply Last reply
        0
        • L lansing

          @mrjj

          Hi the slots can be a boolean or a void with no arguments.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @lansing
          Ok
          Then its a more complicated.
          Maybe just use SLOT and old syntax for that connect unlike you feeling digging into templates and fun stuff?

          L 1 Reply Last reply
          0
          • mrjjM mrjj

            @lansing
            Ok
            Then its a more complicated.
            Maybe just use SLOT and old syntax for that connect unlike you feeling digging into templates and fun stuff?

            L Offline
            L Offline
            lansing
            wrote on last edited by
            #5

            @mrjj
            How do I use the SLOT one, I want the easy solution.

            mrjjM 1 Reply Last reply
            0
            • L lansing

              @mrjj
              How do I use the SLOT one, I want the easy solution.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @lansing
              Then its just to store a const char *

              struct ActionSlotPack {
                 QAction * pAction;
                 const char * connectionSlot;
              };
              
              ...
              ActionSlotPack actionSlots[] =
                     {
                         {newAct, SLOT(slotNew())},
                         {openAct, SLOT(slotOpen())}
                     };
              
              
              1 Reply Last reply
              1

              • Login

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