Using PyQt signals correctly

Posted by Skilldrick on Stack Overflow See other posts from Stack Overflow or by Skilldrick
Published on 2010-04-12T13:02:03Z Indexed on 2010/04/12 13:03 UTC
Read the original article Hit count: 530

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about pyqt

Related posts about signals-slots