0

我在将数据从一个 QWizard 页面发送到下一个页面时遇到问题。我使用my_nameQWizard 对象的变量作为容器。我的方法是:每当我更改 QLineEdit on 的文本时,我的 QWizard 对象Page1的变量就会更改。my_name而且每当我点击下一步按钮时Page1Page2都会使用该方法进行初始化QWizard.initializePage(2)。但是 QLabel 对象Page2不是基于my_nameQWizard 对象的变量更新的。即使我已经初始化了Page2也。我的方法有什么问题?

我的代码是:

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())
4

1 回答 1

1

更改变量“my_name”的值不会更改 QLabel 显示的内容,因为 QLabel 复制了文本。另一方面,您不应该调用initializePage(2),因为它是在内部调用的受保护方法。解决方法是重写QWizardPage的initializePage方法:

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}')

尽管我看到您正在重新发明轮子,因为已经有注册字段的特征:

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()
于 2020-11-13T13:30:36.370 回答