Skip to content Skip to sidebar Skip to footer

How To Properly Initialize A Qwizard Page?

I am having problems with sending data from one QWizard page to the next. I'm using a variable my_name of QWizard object as a container. My approach is: whenever I change text of Q

Solution 1:

Changing the value of the variable "my_name" does not change what the QLabel shows since QLabel copies the text. On the other hand you should not call initializePage(2) since it is a protected method that is called internally. The solution is to override the initializePage method of the QWizardPage:

classPage2(QWizardPage):
    def__init__(self, parent=None):
        super(Page2, self).__init__()
        self.Parent = parent

        vbox = QVBoxLayout(self)
        self.label = QLabel()
        self.label.setText(f'My name is : {self.Parent.my_name}')
        vbox.addWidget(self.label)

    definitializePage(self):
        self.label.setText(f'My name is : {self.Parent.my_name}')

Although I see that you are reinventing the wheel since there is already that characteristic registering the fields:

classWindow(QWizard):
    def__init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.firstPage = MainPage()
        self.secondPage = Page2()

        self.addPage(self.firstPage)
        self.addPage(self.secondPage)


classMainPage(QWizardPage):
    def__init__(self, parent=None):
        super(MainPage, self).__init__(parent)

        self.setTitle("Plz input your name?")

        self.NameLabel = QLabel("&Name:")
        self.NameLineEdit = QLineEdit()
        self.NameLabel.setBuddy(self.NameLineEdit)

        layout = QHBoxLayout(self)
        layout.addWidget(self.NameLabel)
        layout.addWidget(self.NameLineEdit)

        self.registerField("my_name", self.NameLineEdit)


classPage2(QWizardPage):
    def__init__(self, parent=None):
        super(Page2, self).__init__(parent)

        vbox = QVBoxLayout(self)
        self.label = QLabel()
        vbox.addWidget(self.label)

    definitializePage(self):
        self.label.setText(f'My name is : {self.field("my_name")}')
        super(Page2, self).initializePage()

Post a Comment for "How To Properly Initialize A Qwizard Page?"