Reply
Thread Tools
Posts: 341 | Thanked: 57 times | Joined on Nov 2009
#1
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 ..
 
Posts: 341 | Thanked: 57 times | Joined on Nov 2009
#2
 
mikec's Avatar
Posts: 1,366 | Thanked: 1,185 times | Joined on Jan 2006
#3
You need to group your buttons using Qbutton group. Then you can enquire the button id that caused your signal.

Mike C
__________________
N900_Email_Options Wiki Page
 
Posts: 70 | Thanked: 39 times | Joined on Mar 2010
#4
Code:
def __init__(self):
    ...
    self.connect(button1, SIGNAL("pressed()"), lambda: self.pressedNum(1))
    ...
Code:
def pressedNum(self, num):
    self.computeFactorial(num)
 

The Following 2 Users Say Thank You to cheungs For This Useful Post:
Posts: 415 | Thanked: 732 times | Joined on Jan 2009 @ Finland
#5
You can connect multiple signals to the same slot. No special tricks needed.

One possible solution is to set object names for the buttons and identify the clicked button in the slot using QObject::sender()->objectName()

http://doc.qt.nokia.com/latest/qobject.html#sender
http://doc.qt.nokia.com/latest/qobje...bjectName-prop
 
Reply


 
Forum Jump


All times are GMT. The time now is 09:11.