pythonpyqt5qt-signals

pyqt5 connecting button press to a stackedWidget


I just converted my .ui file into the py file and tried to link a QPushButton to a QStackedWidget using the signals and slots system. This is what I attempted to do:

self.createButton.clicked.connect(self.stackedWidget.setCurrentIndex(2)) 

and it was supposed to set the index of the stacked widget to 2 on button click. However at runtime the following error occurred:

TypeError: argument 1 has unexpected type 'NoneType'

Was not really sure what argument 1 was, but the placeholder code of:

self.createButton.clicked.connect(self.stackedWidget.update)

seemed to work except that was not what I want to achieve. What is wrong with my code?


Solution

  • the the connect function takes as a parameter a function to call when the click event is emited so you should wrap your statment in a lambda function as follows :

    self.createButton.clicked.connect(lambda: self.stackedWidget.setCurrentIndex(2))