Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value of a widget without triggering valueChanged callback in PySide?

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

Edit

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)
like image 893
ceremcem Avatar asked Jan 25 '16 10:01

ceremcem


1 Answers

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)
like image 103
Achayan Avatar answered Oct 22 '22 14:10

Achayan