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. How to connect Slot have more argument than Singal?
Qt 6.11 is out! See what's new in the release blog

How to connect Slot have more argument than Singal?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.5k 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.
  • K Offline
    K Offline
    Kien Bui
    wrote on last edited by
    #1

    Example:

    functionSlot(int,int,MyClass *obj);
    ....
    AnyFunction(){
        ..
        connect(sender, SIGNAL(started()), this, SLOT(functionSlot(int,int,MyClass*)));
        ..
    }
    
    
    jsulmJ Taz742T 2 Replies Last reply
    0
    • K Kien Bui

      Example:

      functionSlot(int,int,MyClass *obj);
      ....
      AnyFunction(){
          ..
          connect(sender, SIGNAL(started()), this, SLOT(functionSlot(int,int,MyClass*)));
          ..
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kien-Bui With this syntax you can't. You can use a lambda as slot which then calls the actual method passing default parameters.
      But the question is why do you want to do this and what values should all these parameters have if the signal does not provide them?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Pablo J. RoginaP 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Kien-Bui With this syntax you can't. You can use a lambda as slot which then calls the actual method passing default parameters.
        But the question is why do you want to do this and what values should all these parameters have if the signal does not provide them?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @Kien-Bui in addition to @jsulm reply, regarding slots usually matching number of parameters from signal (as the slot is supposed to do something with that data), if you really end up needing more parameters in slot than what the signal provides, I imagine having an interim slot receiving what the signal sends, and then calling the method that actually will do the work, which receives the additional data. Pseudo-code for your example:

        connect(sender, SIGNAL(started()), this, SLOT(functionSlot()));
        ...
        void ClassX::functionSlot() {
            // slot used as indirection...
            int x,y;
            MyClass* object = new MyClass();
            anotherMethod(x, y, object);
        }
        ...
        void ClassX::anotherMethod(int, int, MyClass*) {
              // do the actual work...
        }
        

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        K 1 Reply Last reply
        2
        • K Kien Bui

          Example:

          functionSlot(int,int,MyClass *obj);
          ....
          AnyFunction(){
              ..
              connect(sender, SIGNAL(started()), this, SLOT(functionSlot(int,int,MyClass*)));
              ..
          }
          
          
          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by Taz742
          #4

          @jsulm said in How to connect Slot have more argument than Singal?:

          With this syntax you can't. You can use a lambda as slot which then calls the actual method passing default parameters.

          With lambda looks like:

          connect(sender, &Sender::started, this, [=]() {
                 functionSlot(, , ,);
          });
          

          Do what you want.

          1 Reply Last reply
          2
          • Pablo J. RoginaP Pablo J. Rogina

            @Kien-Bui in addition to @jsulm reply, regarding slots usually matching number of parameters from signal (as the slot is supposed to do something with that data), if you really end up needing more parameters in slot than what the signal provides, I imagine having an interim slot receiving what the signal sends, and then calling the method that actually will do the work, which receives the additional data. Pseudo-code for your example:

            connect(sender, SIGNAL(started()), this, SLOT(functionSlot()));
            ...
            void ClassX::functionSlot() {
                // slot used as indirection...
                int x,y;
                MyClass* object = new MyClass();
                anotherMethod(x, y, object);
            }
            ...
            void ClassX::anotherMethod(int, int, MyClass*) {
                  // do the actual work...
            }
            
            K Offline
            K Offline
            Kien Bui
            wrote on last edited by
            #5

            @Pablo-J.-Rogina said in How to connect Slot have more argument than Singal?:

            @Kien-Bui in addition to @jsulm reply, regarding slots usually matching number of parameters from signal (as the slot is supposed to do something with that data), if you really end up needing more parameters in slot than what the signal provides, I imagine having an interim slot receiving what the signal sends, and then calling the method that actually will do the work, which receives the additional data. Pseudo-code for your example:

            connect(sender, SIGNAL(started()), this, SLOT(functionSlot()));
            ...
            void ClassX::functionSlot() {
                // slot used as indirection...
                int x,y;
                MyClass* object = new MyClass();
                anotherMethod(x, y, object);
            }
            ...
            void ClassX::anotherMethod(int, int, MyClass*) {
                  // do the actual work...
            }
            

            but MyClass was initialized before.

            MyClass obj = new MyClass();
            connect(sender, SIGNAL(started(int, int)),  [&](int a, int b){functionSlot(a,b,obj); });
            
            Taz742T 1 Reply Last reply
            0
            • K Kien Bui

              @Pablo-J.-Rogina said in How to connect Slot have more argument than Singal?:

              @Kien-Bui in addition to @jsulm reply, regarding slots usually matching number of parameters from signal (as the slot is supposed to do something with that data), if you really end up needing more parameters in slot than what the signal provides, I imagine having an interim slot receiving what the signal sends, and then calling the method that actually will do the work, which receives the additional data. Pseudo-code for your example:

              connect(sender, SIGNAL(started()), this, SLOT(functionSlot()));
              ...
              void ClassX::functionSlot() {
                  // slot used as indirection...
                  int x,y;
                  MyClass* object = new MyClass();
                  anotherMethod(x, y, object);
              }
              ...
              void ClassX::anotherMethod(int, int, MyClass*) {
                    // do the actual work...
              }
              

              but MyClass was initialized before.

              MyClass obj = new MyClass();
              connect(sender, SIGNAL(started(int, int)),  [&](int a, int b){functionSlot(a,b,obj); });
              
              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by Taz742
              #6

              @Kien-Bui said in How to connect Slot have more argument than Singal?:

              connect(sender, SIGNAL(started(int, int)), [&](int a, int b){functionSlot(a,b,obj); });

              This is wrong syntax. See my answer and this http://wiki.qt.io/New_Signal_Slot_Syntax.

              Do what you want.

              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