使用 IndependentPages 选项时,根据来自其他页面的信息更新 QWizardPage 上的信息

Update information on QWizardPage based on information from other pages when IndependentPages option is used

我想构建一个 QWizard,它在一些初始页面中获取信息,然后在最后一页上汇总这些信息。用户应该能够返回并更新初始页面上的值,而不会丢失信息。因此我使用 QWizard.IndependentPages 选项。这可以防止 Qt 运行 cleanupPage() 清除先前输入的数据。但是,问题是 initializePage() 也仅在第一次访问页面时被调用。

我似乎找不到一个干净的方法来让最后一页上的信息始终根据之前页面上输入的内容进行更新。

我附上了一小段代码,共 3 页,以按原样说明功能。当导航到第二页时,输入数据,然后在第3页查看此数据。当返回第2页时,仍然显示之前的数据(根据需要),当导航到第1页再到第2页时,仍然是初始数据显示数据(根据需要)。然后更改数据操作页面 2,用户移动到页面 3,仍然显示旧数据。这是因为没有调用构造函数或 initializePage() 函数,并且没有任何内容触发此标签的更新。

from os import terminal_size
import sys
import csv
from PyQt5 import QtCore

from PyQt5.QtCore import QDir
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QFrame, QWizard, QWizardPage, QVBoxLayout, QLabel, QFormLayout, QRadioButton, QLineEdit, QComboBox, \
    QGroupBox, QApplication, QCheckBox, QHBoxLayout, QScrollArea, QWidget



wizard_page_index = {
    "IntroPage": 0,
    "EvalPage": 1,
    "RegisterPage": 2
}


TEST = ""

class LicenseWizard(QWizard):
    def __init__(self):
        super().__init__()
        self.setPage(wizard_page_index["IntroPage"], IntroPage())
        self.setPage(wizard_page_index["EvalPage"], EvalPage())
        self.setPage(wizard_page_index["RegisterPage"], RegisterPage())

        self.setStartId(wizard_page_index["IntroPage"])

        self.setWindowTitle("Test Wizard")
        self.setOption(QWizard.IndependentPages)



class IntroPage(QWizardPage):
    def __init__(self):
        super().__init__()

        self.setTitle("Intro Page")

        data_source_layout = QFormLayout() 

        eval_btn = QRadioButton("evaluate")
        self.registerField("evaluate_btn", eval_btn)
        data_source_layout.addWidget(eval_btn)

        register_btn = QRadioButton("register")
        self.registerField("register_btn", register_btn)
        data_source_layout.addWidget(register_btn)

        register_btn.setChecked(True)

        layout = QVBoxLayout()
        layout.addWidget(eval_btn)
        layout.addWidget(register_btn)

        self.setLayout(layout)

    def nextId(self) -> int:
        if self.field("evaluate_btn"):
            return wizard_page_index["EvalPage"]
        else:
            return wizard_page_index["RegisterPage"]
            

class EvalPage(QWizardPage):
    def __init__(self):
        super().__init__()
        self.setTitle("EvalPage")

        layout = QFormLayout()
        model_name = QLineEdit(TEST)
        layout.addRow("Model Name:", model_name)
        self.registerField("model_name*", model_name)

        self.setLayout(layout)

    def nextId(self) -> int:
        return wizard_page_index["RegisterPage"]



class RegisterPage(QWizardPage):
    def __init__(self):
        super().__init__()
        self.setTitle("RegisterPage")
        self.label2 = QLabel()
        self.label2.setFrameStyle(QFrame.Panel)
        self.label2.setText(self.field("model_name"))

        layout = QFormLayout()
        
        layout.addRow("name:", self.label2)

        self.setLayout(layout)

        

    def initializePage(self) -> None:
        self.label2.setText(self.field("model_name"))
        

    def nextId(self) -> int:
    #    self.label2.setText(self.field("model_name"))
        return wizard_page_index["EvalPage"]



if __name__ == "__main__":

    application = QApplication(sys.argv)
    wizard = LicenseWizard()
    wizard.show()
    return_code = application.exec_()

    
    input('Press ENTER to exit')
    sys.exit(return_code)

一个可能的解决方案是不使用 QWizard::IndependentPages 并覆盖 QWizard::cleanupPage() 方法:

class LicenseWizard(QWizard):
    def __init__(self):
        super().__init__()
        self.setPage(wizard_page_index["IntroPage"], IntroPage())
        self.setPage(wizard_page_index["EvalPage"], EvalPage())
        self.setPage(wizard_page_index["RegisterPage"], RegisterPage())

        self.setStartId(wizard_page_index["IntroPage"])

        self.setWindowTitle("Test Wizard")
        # self.setOption(QWizard.IndependentPages)

    def cleanupPage(self, _id):
        pass