Search code examples
pythonsignals-slotspyside

Signals and slots in PySide - defining slot in class


I'm new to Qt and PySide and try to get a feeling for signals and slots in PySide.

While following the documentation of Signals and Slots in PySide, I tried to transfer some code to be used in my class.

The following code creates a DoubleSpinBox. When changing the value I expect the value to be printed by both functions value_changed_func and value_changed_class, but only value_changed_func gets called.

from PySide import QtCore
from PySide import QtGui

@QtCore.Slot(float)
def value_changed_func(value):
    print "Event func:"
    print value

class MainController(QtCore.QObject):

    def __init__(self, parent):
        self._ui = QtGui.QDoubleSpinBox(parent)

        self._ui.valueChanged.connect(self.value_changed_class)
        self._ui.valueChanged.connect(value_changed_func)

    @QtCore.Slot(float)
    def value_changed_class(self, value):
        print "Event class:"
        print value


app = QtGui.QApplication([])

main_window = QtGui.QMainWindow()
MainController(main_window)
main_window.show()

app.exec_()

What am I doing wrong? How do I get value_changed_class to be called?


Solution

  • You didn't call the parent constructor:

    def __init__(self, parent):
        super(MainController, self).__init__(parent)
        ...