PyQt connects SIGNAL to multiple SLOTs

I am trying to learn PyQt from rapid gui programming with python and qt and am currently learning Signals and Slots .

The following is a short snippet of code:

 self.connect(self.dial, SIGNAL("valueChanged(int)"),self.spinbox.setValue) #1 self.connect(self.dial, SIGNAL("valueChanged(int)"),self.getValue_dial) #2 self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.dial.setValue) self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.getValue_spinbox) def getValue_dial(self): print self.dial.value() def getValue_spinbox(self): print self.dial.value() 

What I'm trying to achieve here is calling 2 SLOTS right away, which is spinbox.setValue and getValue_dial for the dial object as soon as the ValueChanged(int) signal is issued.

The above code runs successfully without errors and print corresponding values ​​as they change.

My question now is that you can name several slots for one signal.

Is it possible to combine the two above statements (1 and 2) into one operator.

Here is a link to my full code .

+4
source share
3 answers

The way you do it is wonderful. If you had a lot of things, you could connect to a new function that handles everything for you.

I noticed that in your connected getValue functions getValue you get the value directly from the object; did you know that the value is passed as a parameter with the signal valueChanged(int) ? If you change your getValue functions to accept an additional parameter, you will not need to get the value directly from the object. Of course, you can end the getValue function all together and issue a print statement in a helper function.

 self.connect(self.dial, SIGNAL("valueChanged(int)"), self.dial_value_changed) self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.spinbox_value_changed) def dial_value_changed(self, value): self.spinbox.setValue(value) self.getValue_dial(value) def spinbox_value_changed(self, value): self.dial.setValue(value) self.getValue_spinbox(value) def getValue_dial(self, value): print value def getValue_spinbox(self, value): print value 

In addition, and it depends on preference, there is a new style for signals and slots that can make the code a little easier to read. It will change

 self.connect(self.dial, SIGNAL("valueChanged(int)"), self.dial_value_changed) self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.spinbox_value_changed) 

higher than

 self.dial.valueChanged.connect(self.dial_value_changed) self.spinbox.valueChanged.connect(self.spinbox_value_changed) 

But for the original question and for the two things you are doing, I just connected the signal twice, instead of having an auxiliary function.

+10
source

You can use the list to connect two slots / functions in one expression:

 # in Python 2.X map(self.dial.valueChanged.connect, [self.spinbox.setValue, self.getValue_dial]) # in Python 3.X (map returns an iterator instead of processing the list) list(map(self.dial.valueChanged.connect, [self.spinbox.setValue, self.getValue_dial])) # or with any Python version [self.dial.valueChanged.connect(x) for x in [self.spinbox.setValue, self.getValue_dial]] 
+4
source

This is a good way, yes. You can't combine two CONNECT statements into one.

+1
source

Source: https://habr.com/ru/post/1413166/


All Articles