0

我想构建一个 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)
4

1 回答 1

1

一种可能的解决方案是不使用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
于 2021-12-08T20:47:03.397 回答