PDA

View Full Version : Can I connect multiple buttons to same slot, but pass different parameters ?



ahmadka
12th June 2010, 00:21
Hey guys .. My question here is regarding signals and slots ..

For example, say you have like 10 buttons, all of which pass a number into a factorial function .. the only difference is that they all pass a different number into this one factorial function ... suppose the function is:

int factorial (int a);

so for example, button1 evaluates this function where a =1,
so for example, button2 evaluates this function where a =2,
so for example, button3 evaluates this function where a =3,

and so on ..

In such a case, is there any way to connect these multiple 'clicked' signals from each individual button to the same factorial slot, but just pass in a different value for a .. ? The alternative is to make a connect statement for each individual button, which seems rather inefficient considering they all do more or less the same thing ..

SixDegrees
12th June 2010, 00:37
There are several ways to do this, but the most logical way, from a design standpoint, would be to extend your button to emit a signal with an integer argument. This signal can be connected to the button's clicked() signal. The data itself can be stored in the new button class, or simply extracted from the button's label and converted to an integer.

wysota
12th June 2010, 02:21
A good alternative would be to use QSignalMapper but you'll still need to do a separate connection for each button.