Proper use of PyQt signals

Some time ago I did some work in Qt for C ++; I am now working with PyQt.

I have a subclass of QStackedWidget and inside this subclass of QWidget. In QWidget, I want to click a button that jumps to the next QStackedWidget page. 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 connect the continueButton clickedsignal to the EventsPage signal nextPage, which then connects to Stacked in the method nextPage. I could just delve into the internal elements of EventsPage in Stacked and plug in self.widget1.continueButton.clicked, but this seemed to completely destroy the purpose of the signals and slots.

So does this approach make sense, or is there a better way?

+3
1

, . , . ( ...), , . , , .

+6

Source: https://habr.com/ru/post/1740764/


All Articles