+7 votes
by (13.6m points)

I have several QDoubleSpinBoxes and I want connect them to one Slot. It is possible to make one connect-command, for more than one object? for exampe, I want to connect:

doubleSpinBox_1
doubleSpinBox_2

to my function "bla". Is there any command like:

self."doubleSpinBox_1 **AND** _2".valueChanged.connect(self.bla)

?

2 Answers

+2 votes
by (13.6m points)

Alternatively, you could use getattr :

for id in range(1,3):
    spinbox = getattr(self, "doubleSpinBox_{}".format(id))
    spinbox.valueChanged.connect(self.mySlot)
+1 vote
by (13.6m points)

Try this:

for spin_id in range(1,3):
  spinboxes = self.findChildren(QtGui.QDoubleSpinBox, "doubleSpinBox_%d"%spin_id)
  if spinboxes:
    spinboxes[0].valueChanged.connect(self.bla)

But this code is useful if you have many spinboxes to connect ;)

...