I want to update a widget's value but in order to prevent infinite loops, I need to prevent calling the callback function of this widget's .valueChanged
signal.
Following example works entirely as expected:
Qt = PySide
class MainWindow(Actor, Qt.QtGui.QMainWindow):
def __init__(self):
Qt.QtGui.QMainWindow.__init__(self)
Actor.__init__(self)
self.ui = Qt.loadUI('simulator.ui')
self.ui.horizontalSlider.valueChanged.connect(self.emit_weight_msg)
def emit_weight_msg(self, value):
self.send({'WeightMessage': {'val': value}})
def handle_WeightMessage(self, msg):
self.ui.horizontalSlider.valueChanged.disconnect(self.emit_weight_msg)
self.ui.horizontalSlider.setValue(msg["val"])
self.ui.horizontalSlider.valueChanged.connect(self.emit_weight_msg)
Since disconnecting and connecting back the valueChanged
signals seems a bit like hack, I want to ask if there is a more elegant solution exists.
Full code is here: https://github.com/ceremcem/weighing-machine-testing
I'm looking for a method, like:
def setValueSilent(QtObject, value):
tmp_callback = QtObject.get_callback_functions_that_are_set_in_python_code()
QtObject.valueChanged.disconnect(tmp_callback)
QtObject.setValue(value)
QtObject.valueChanged.connect(tmp_callback)
I think if you use blocksignals then it should be fine.
def handle_WeightMessage(self, msg):
self.ui.horizontalSlider.blockSignals(True)
self.ui.horizontalSlider.setValue(msg["val"])
self.ui.horizontalSlider.blockSignals(False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With