qtpyqt5qwizard

Open a new window after Finish Button has been clicked on Qwizard : Pyqt5


I want to have the finish button on my QWizard do something else besides exit the page. I need to connect it to a function that calls another window. In other words, I need to view and add functionality to the Finish Button of the Qwizard page. Does anyone know how to do this. Thanks


Solution

  • Its mostly the same you are already used to do in PyQt. The differences are in how to find the Finish button entity. Here is a working example:

    import sys
    from PyQt5 import QtWidgets, QtCore, QtGui
    
    class IntroPage(QtWidgets.QWizardPage):
        def __init__(self, parent=None):
            super(IntroPage, self).__init__(parent)
    
    class LastPage(QtWidgets.QWizardPage):
        def __init__(self, parent=None):
            super(LastPage, self).__init__(parent)
    
    class MyWizard(QtWidgets.QWizard):
        def __init__(self, parent=None):
            super(MyWizard, self).__init__(parent)
    
            self.introPage = IntroPage()
            self.lastPage = LastPage()
            self.setPage(0, self.introPage)
            self.setPage(1, self.lastPage)
    
            # This is the code you need
            self.button(QtWidgets.QWizard.FinishButton).clicked.connect(self._doSomething)
    
        def _doSomething(self):
            msgBox = QtWidgets.QMessageBox()
            msgBox.setText("Yep, its connected.")
            msgBox.exec()
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        main = MyWizard()
        main.show()
        sys.exit(app.exec_())
    

    Notice how I used this command: self.button(QtWidgets.QWizard.FinishButton) to specifically point to the finish button. The rest is just build your own method to do whatever you need. In my example I connected to def _doSomething(self) and launched a very simple QMessageBox.