c++qtargumentssignals-slotsqsignalmapper

How to place my function using QSignalMapper and QObject::connect


I'm a beginner in c++.

i'm trying to pass an argument using QSignalMapper. I do something like this:

int main(int argc, char** argv)
{
    ...
    QSignalMapper * mapper = new QSignalMapper(0);
    QObject::connect(mapper,SIGNAL(mapped(int )), 0 ,SLOT(mySlot(int )));

    int prova=11;
    mapper->setMapping(but, prova);
    QObject::connect(but, SIGNAL(clicked()),mapper,SLOT(map()));
    
   //do stuff
}

Where i can put mySlot()? I need to pass the variable "prova" Thanks to all.


Solution

  • Forget about QSignalMapper and use lambdas:

    QObject::connect(but, &QButton::clicked, myObject, [myObject,prova]() { myObject->mySlot(prova); });
    

    In case mySlot is just a regular function:

    QObject::connect(but, &QButton::clicked, [prova]() { mySlot(prova); });