How to connect Slot have more argument than Singal?



  • Example:

    functionSlot(int,int,MyClass *obj);
    ....
    AnyFunction(){
        ..
        connect(sender, SIGNAL(started()), this, SLOT(functionSlot(int,int,MyClass*)));
        ..
    }
    
    

  • Moderators

    @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?



  • @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...
    }
    


  • @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(, , ,);
    });
    


  • @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); });
    


  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.