0

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.

JarMan
  • 7,589
  • 1
  • 10
  • 25
Pola
  • 33
  • 5
  • I think you need to explain what you are trying to do more. I don't understand the issue. – drescherjm Jun 22 '22 at 17:32
  • when i click on button "but", i want to send to mySlot() an int value in order to do some calculations – Pola Jun 23 '22 at 09:34
  • The mapping will send a fixed value to the slot for the last time you executed setMapping(). I am curious where and how the `prova` value gets set. – drescherjm Jun 23 '22 at 12:35

1 Answers1

2

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); });
Botje
  • 26,269
  • 3
  • 31
  • 41
  • Thank you for your answer. I'm try to implement your solution. What i have to put in myObject? Then, can you please link me some documentations? thank you! – Pola Jun 23 '22 at 09:42
  • `myObject` would be an instance of an object that hosts `mySlot`. If it is just a plain function, see edit. – Botje Jun 23 '22 at 11:17
  • i've implemented your information in the main class: ` QObject::connect(but, &QPushButton::clicked, [ prova]() { }); ` just like this and it works! do you think is good? thank you so much! – Pola Jun 23 '22 at 11:21