I have a widget and inside it are 32 buttons. I need to connect each button's pressed() signal to a slot in order to call a function who's parameters depend on which button I have pressed. Right now I did that by adding 32 slots in the form of on_QPushButtonName_pressed() but thats a lot of slots. I was wondering if there is another way I could do it that is smaller. I have done something similar but I was working with custom widgets so I could just create a new signal in the code of my class but I would like to avoid creating a custom widget for just a single button.
views:
121answers:
2
+8
A:
Use the QSignalMapper class. The documentation - http://doc.trolltech.com/4.5/qsignalmapper.html - has an example pretty close to what you want.
Intransigent Parsnip
2009-11-26 02:14:22
thx, i'm pretty sure this is what I need
yan bellavance
2009-11-26 02:21:50
yep, everything worked out thx again
yan bellavance
2009-11-26 03:08:07
A:
Another possibility: creating just one slot, calling sender()
and switching on the result.
As Rohan mentioned, QSignalMapper is the recommended solution, since sender() is a bit of a hack. Its advantage is that it's easier to use.
rpg
2009-11-26 16:38:20
QSignalMapper is built around sender, so if you use it, you can be sure that the Qt developers will take it into account if sender changes behaviour.
e8johan
2009-11-27 10:19:24