How to properly initialize a QWizard page?

后端 未结 1 1140
难免孤独
难免孤独 2021-01-27 23:55

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

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 00:27

    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:

    class Page2(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)
    
        def initializePage(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:

    class Window(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)
    
    
    class MainPage(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)
    
    
    class Page2(QWizardPage):
        def __init__(self, parent=None):
            super(Page2, self).__init__(parent)
    
            vbox = QVBoxLayout(self)
            self.label = QLabel()
            vbox.addWidget(self.label)
    
        def initializePage(self):
            self.label.setText(f'My name is : {self.field("my_name")}')
            super(Page2, self).initializePage()
    

    0 讨论(0)
提交回复
热议问题