问题
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 QLineEdit on Page1
, the variable my_name
of my QWizard object changes. And whenever I click Next button on Page1
, Page2
is initialized using the method QWizard.initializePage(2)
. But the QLabel object on Page2
is not update based on the my_name
variable of QWizard object. Even though I have initialized the Page2
also. What is wrong with my approach?
My code is:
import sys
from PyQt5.QtWidgets import *
class Window(QWizard):
def __init__(self):
super(Window, self).__init__()
self.firstPage = MainPage(parent=self)
self.my_name = 'Random'
self.secondPage = Page2(parent=self)
self.addPage(self.firstPage)
self.button(QWizard.NextButton).clicked.connect(lambda: self.initializePage(2))
self.addPage(self.secondPage)
class MainPage(QWizardPage):
def __init__(self, parent=None):
self.Parent = parent
super(MainPage, self).__init__(parent)
self.setTitle("Plz input your name?")
self.NameLabel = QLabel("&Name:")
self.NameLineEdit = QLineEdit()
self.NameLineEdit.textChanged.connect(self.assign)
self.NameLabel.setBuddy(self.NameLineEdit)
layout = QHBoxLayout()
layout.addWidget(self.NameLabel)
layout.addWidget(self.NameLineEdit)
self.setLayout(layout)
def assign(self):
self.Parent.my_name = self.NameLineEdit.text()
print(f'Parent text is: {self.Parent.my_name}')
class Page2(QWizardPage):
def __init__(self, parent=None):
super(Page2, self).__init__()
self.Parent = parent
vbox = QVBoxLayout()
label = QLabel()
label.setText(f'My name is : {self.Parent.my_name}')
vbox.addWidget(label)
self.setLayout(vbox)
def main():
app = QApplication(sys.argv)
app.setStyle('plastique')
window = Window()
window.setWizardStyle(1)
window.show()
app.exec_()
if __name__ == "__main__":
sys.exit(main())
回答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:
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()
来源:https://stackoverflow.com/questions/64817652/how-to-properly-initialize-a-qwizard-page