I am trying to implement a Qwizard application using PyQt5 and I cannot figure out how to call different functions based on which next button is clicked. Apparently, next buttons belong to the Qwizard class and I cannot define specific behavior (like calling different functions) for them on each page. Here is an example:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Window(QWizard):
def __init__(self):
super(Window, self).__init__()
self.firstPage = Page1()
self.secondPage = Page2()
self.thirdPage = Page3()
self.addPage(self.firstPage)
self.addPage(self.secondPage)
self.addPage(self.thirdPage)
class Page1(QWizardPage):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.setTitle('''Page 1 Title''')
self.vLayout = QVBoxLayout()
self.lineEdit = QLineEdit()
self.vLayout.addWidget(self.lineEdit)
self.setLayout(self.vLayout)
self.registerField('p1_lineEdit*', self.lineEdit, self.lineEdit.text(), self.lineEdit.textChanged)
def f1(self):
print("f1")
if(len(self.lineEdit.text()) < 10):
self.lineEdit.setText("0" * (10 - len(self.lineEdit.text())) + self.lineEdit.text())
def validatePage(self):
self.f1()
return super(Page1, self).validatePage()
class Page2(QWizardPage):
def __init__(self):
super(Page2, self).__init__()
self.setTitle('''Page 2 Title''')
self.vLayout = QVBoxLayout()
self.textBrowser = QTextBrowser()
self.vLayout.addWidget(self.textBrowser)
text = f'''Please make sure the provided number: {self.field('p1_lineEdit')} that you previously indicated, is valid.'''
self.textBrowser.setText(text)
self.setLayout(self.vLayout)
def f2(self):
print("f2")
print(self.field('p1_lineEdit'))
def validatePage(self):
self.f2()
return super(Page2, self).validatePage()
class Page3(QWizardPage):
def __init__(self):
super(Page3, self).__init__()
self.setTitle('''Page 3 Title''')
def f3(self):
print("f3")
def validatePage(self):
self.f3()
return super(Page3, self).validatePage()
def main():
app = QApplication(sys.argv)
app.setStyle('plastique')
window = Window()
window.setWizardStyle(1)
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())
How can I differentiate next button behavior based on QwizardPage rather than Qwizard? Let's say I want f1() be called after pressing next button on page1, f2 after pressing next button on page 2 and f3() after page3.
UPDATE: I am adding the edited question following the guides about minimal reproducible example and XY problem. I'm trying to validate user inputs on the specific wizard pages after the next button is clicked and use those for populating subsequent pages. Here I'm asking for an input on page 1, pretending it if short and inserting it in the middle of a text in a text browser on page 2. However, I see None in place of registered field p1_lineEdit inside the text browser on page 2, but when I print it in f2() before moving to page 3, I see the correct value. I want the user to observe the correct value when page 2 loads.
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Window(QWizard):
def __init__(self):
super(Window, self).__init__()
self.firstPage = Page1()
self.secondPage = Page2()
self.thirdPage = Page3()
self.addPage(self.firstPage)
self.addPage(self.secondPage)
self.addPage(self.thirdPage)
class Page1(QWizardPage):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.setTitle('''Page 1 Title''')
self.vLayout = QVBoxLayout()
self.lineEdit = QLineEdit()
self.vLayout.addWidget(self.lineEdit)
self.setLayout(self.vLayout)
self.registerField('p1_lineEdit*', self.lineEdit, self.lineEdit.text(), self.lineEdit.textChanged)
def f1(self):
print("f1")
if(len(self.lineEdit.text()) < 10):
self.lineEdit.setText("0" * (10 - len(self.lineEdit.text())) + self.lineEdit.text())
def validatePage(self):
self.f1()
return super(Page1, self).validatePage()
class Page2(QWizardPage):
def __init__(self):
super(Page2, self).__init__()
self.setTitle('''Page 2 Title''')
self.vLayout = QVBoxLayout()
self.textBrowser = QTextBrowser()
self.vLayout.addWidget(self.textBrowser)
text = f'''Please make sure the provided number: {self.field('p1_lineEdit')} that you previously indicated, is valid.'''
self.textBrowser.setText(text)
self.setLayout(self.vLayout)
def f2(self):
print("f2")
print(self.field('p1_lineEdit'))
def validatePage(self):
self.f2()
return super(Page2, self).validatePage()
class Page3(QWizardPage):
def __init__(self):
super(Page3, self).__init__()
self.setTitle('''Page 3 Title''')
def f3(self):
print("f3")
def validatePage(self):
self.f3()
return super(Page3, self).validatePage()
def main():
app = QApplication(sys.argv)
app.setStyle('plastique')
window = Window()
window.setWizardStyle(1)
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())