如何正确初始化 QWizard 页面? [英] How to properly initialize a QWizard page?
问题描述
我在将数据从一个 QWizard 页面发送到下一个页面时遇到问题.我使用 QWizard 对象的变量 my_name
作为容器.我的方法是:每当我在 Page1
上更改 QLineEdit 的文本时,我的 QWizard 对象的变量 my_name
都会更改.每当我单击 Page1
上的 Next 按钮时,Page2
都会使用 QWizard.initializePage(2)
方法进行初始化.但是 Page2
上的 QLabel 对象不是基于 QWizard 对象的 my_name
变量更新的.即使我也初始化了 Page2
.我的方法有什么问题?
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?
我的代码是:
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())
推荐答案
更改变量my_name"的值不会更改 QLabel 显示的内容,因为 QLabel 会复制文本.另一方面,您不应调用 initializePage(2)
,因为它是一种在内部调用的受保护方法.解决方法是重写QWizardPage的initializePage方法:
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()
这篇关于如何正确初始化 QWizard 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!