Error if I pass arguments to connect in



  • connect(act, SIGNAL(triggered()), this, SLOT(writeimage(QString("currrent"))));

    gives me error Object::connect: No such slot GQActions::writeimage(QString("all"))
    Object::connect: No such slot GQActions::writeimage(QString("all"))

    but If I do not pass any argument in writeimage then I do not get error
    connect(act, SIGNAL(triggered()), this, SLOT(writeimage());

    it is working fine


  • Moderators

    @Qt-Enthusiast you can not pass parameters to slots in connect!
    Parameters are passed from signal when it is emitted.



  • @Qt-Enthusiast

    As said by @jsulm ,

    connect(act, SIGNAL(triggered()), this, SLOT(writeimage()));
    both the signatures must be same.

    If you have any values to be passed, can provide only datatypes.

    Ex:
    connect(act, SIGNAL(triggered(bool)), this, SLOT(writeimage(bool)));

    Thanks,


  • Moderators

    @Pradeep-Kumar @Qt-Enthusiast Actually the signal can have more parameters than the slot - the slot then just ignores all the other parameters.
    The thing is:

    connect(act, SIGNAL(triggered()), this, SLOT(writeimage(QString("currrent"))));
    

    this is not a valid connect call as QString("currrent") creates a QString instance. If writeimage has a QString parameter then it should be:

    connect(act, SIGNAL(triggered()), this, SLOT(writeimage(QString)));
    

    but even then it is wrong as the signal does not have QString parameter.



  • @jsulm @Pradeep-Kumar @Qt-Enthusiast

    can pass a default parameters to the SIGNAL, to somewhat circumvent the issue

    signals:
       void signalDefault(QString s = "foo");
    
    private slots:
    void slotDefault(QString s){
     // do stuff
    }
    
    //in cpp
    connect(this, SIGNAL(signalDefault(QString)), this, SLOT(slotDefault(QString)));
    emit signalDefault();
    

  • Moderators

    @J.Hilk I never tried, did you?
    But even if it is working I wouldn't do this because you're hiding in your code what you actually pass to the connected slots.



  • @J.Hilk ,

    can we use as u mentioned,

    signals:
    void signalDefault(QString s = "foo");

    and emit without argument?.

    emit signalDefault();

    Thanks,


  • Moderators

    @Pradeep-Kumar just try



  • @jsulm said in Error if I pass arguments to connect in:

    @J.Hilk I never tried, did you?
    But even if it is working I wouldn't do this because you're hiding in your code what you actually pass to the connected slots.

    I did, I have one or 2 such case in my current project, it even work with both new and old SIGNAL/SLOT syntax. I believe that changed recently, as I remember it working only with the old one.



  • @Pradeep-Kumar yes you can.



  • @jsulm , @J-Hilk ,

    yes i tried, i am getting the value in slot,

    so is it the new way, or was present earlier also. just curious.

    Thanks,


  • Moderators

    @Pradeep-Kumar I don't think it's new - it is probably working since C++ allows default values :-)



  • @jsulm

    well i tried for the first time, got the output.

    Thanks,



  • You can use the new connect syntax to make this work:
    connect(act, &QAction::triggered, this, [this]()->void{writeimage(QStringLiteral("currrent"));});


Log in to reply