I am trying to learn PyQt
from rapid gui programming with python and qt
and currently learning Signals
and Slots
.
You can use a list to connect the two slots/functions in a single statement:
# 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]]
The way you're doing it is fine. If you had quite a few things to do you could connect to a new function that handles everything for you.
I notice in your connected getValue
functions you're getting the value straight from the object; are you aware that the value is passed as a parameter with the valueChanged(int)
signal? If you change your getValue
functions to accept an additional parameter there will be no need to get the value directly from the object. Of course you could do away with the getValue
function all together and issue your print statement in the 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
Also, and this is down to preference, there is a new style for signals and slots which can make the code a little easier to read. It would change the
self.connect(self.dial, SIGNAL("valueChanged(int)"), self.dial_value_changed)
self.connect(self.spinbox, SIGNAL("valueChanged(int)"),self.spinbox_value_changed)
lines above to
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're doing I'd just connect the signal twice rather than have a helper function.
This is the appropriate way, yes. You can't combine
the two CONNECT statements into one.