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. Cannot pass parameter in SLOT

Cannot pass parameter in SLOT

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 859 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.
  • G Offline
    G Offline
    GunkutA
    wrote on last edited by GunkutA
    #1

    Hello, I am trying to declare an action in Custom menu Request as:

    void protocolForm::customMenuRequested(QPoint pos)
    {
        QMenu *menu = new QMenu(this);
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(0))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(1))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(2))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(3))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(4))));
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot(5))));
    }
    

    enumArray is Qstring vector. When I try that in this way with integer parameter in sendEnumSlot() slot, that gives me

    QObject::connect: (receiver name: 'widget')
    QObject::connect: No such slot protocolForm::sendEnumSlot(0)
    warnings

    However when I try the same thing with declaring the sendEnumSlot() without any parameters in it and add action as:

    void protocolForm::customMenuRequested(QPoint pos)
    {
        QMenu *menu = new QMenu(this);
        menu->addAction(enumArray.value(0),this,SLOT((sendEnumSlot()));
        menu->addAction(enumArray.value(1),this,SLOT((sendEnumSlot()));
        menu->addAction(enumArray.value(2),this,SLOT((sendEnumSlot()));
        menu->addAction(enumArray.value(3),this,SLOT((sendEnumSlot()));
       menu->addAction(enumArray.value(4),this,SLOT((sendEnumSlot()));
       menu->addAction(enumArray.value(5),this,SLOT((sendEnumSlot()));
    }
    
    

    That works. Am I missing something? How can I overcome this problem?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Signal-slot connections are between methods. You cannot specify concrete values for slot parameters - these are always taken from the signal.

      If you want to call the same slot with different value based on menu item, you can:

      • connect your action to lambda and call your slot with correct parameter inside
      • or call the same slot from all actions, but in the slot call sender() and infer which action triggered the call
      • or create separate slots for each of your actions

      (Z(:^

      G 1 Reply Last reply
      5
      • sierdzioS sierdzio

        Signal-slot connections are between methods. You cannot specify concrete values for slot parameters - these are always taken from the signal.

        If you want to call the same slot with different value based on menu item, you can:

        • connect your action to lambda and call your slot with correct parameter inside
        • or call the same slot from all actions, but in the slot call sender() and infer which action triggered the call
        • or create separate slots for each of your actions
        G Offline
        G Offline
        GunkutA
        wrote on last edited by
        #3

        @sierdzio How can I use sender() function? That returns some address value I guess. How can I know which action refers to that address value?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4
          auto action = qobject_cast<QAction*>(sender());
          if (action) {
            if (action->text() == enumArray.value(0)) {
              // First action triggered the slot
            }
          }
          

          (Z(:^

          G 1 Reply Last reply
          2
          • sierdzioS sierdzio
            auto action = qobject_cast<QAction*>(sender());
            if (action) {
              if (action->text() == enumArray.value(0)) {
                // First action triggered the slot
              }
            }
            
            G Offline
            G Offline
            GunkutA
            wrote on last edited by
            #5

            @sierdzio Thanks

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              @sierdzio There is a fourth option to provide a value to the slot: Qt has a helper class calls QSignalMapper. Haven't used it ever, though ;-)

              1 Reply Last reply
              2

              • Login

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