Using PyQt signals correctly
- by Skilldrick
A while ago I did some work in Qt for C++; now I'm working with PyQt.
I have a subclass of QStackedWidget, and inside that a subclass of QWidget. In the QWidget I want to click a button that goes to the next page of the QStackedWidget. My (simplified) approach is as follows:
class Stacked(QtGui.QStackedWidget):
def __init__(self, parent=None):
QtGui.QStackedWidget.__init__(self, parent)
self.widget1 = EventsPage()
self.widget1.nextPage.connect(self.nextPage)
self.widget2 = MyWidget()
self.addWidget(self.widget1)
self.addWidget(self.widget2)
def nextPage(self):
self.setCurrentIndex(self.currentIndex() + 1)
class EventsPage(QtGui.QWidget):
nextPage = QtCore.pyqtSignal()
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.continueButton = QtGui.QPushButton('Continue')
self.continueButton.clicked.connect(self.nextPage)
So, basically, I'm connecting the continueButton clicked signal to the EventsPage nextPage signal, which I'm then connecting in Stacked to the nextPage method. I could just delve into the internals of EventsPage in Stacked and connect self.widget1.continueButton.clicked, but that seemed to completely defeat the purpose of signals and slots.
So does this approach make sense, or is there a better way?