Python QWizard的父项执行过程中QFrame页面的更改,如何访问QWizard?

Python QWizard的父项执行过程中QFrame页面的更改,如何访问QWizard?,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,我想在QWizard中存储一些数据,然后从QWizardPages访问它。但是,当我尝试从QWizard页面访问数据时,它的父类型在代码执行期间从QWizard更改为QForm,因此我无法再以这种方式访问父数据。为什么会这样?我如何从任何QWizard页面可靠地访问存储在父QWizard中的数据 from PyQt4 import QtGui from PyQt4.QtGui import QLabel, QVBoxLayout class WizardExample(QtGui.QWizar

我想在
QWizard
中存储一些数据,然后从
QWizardPages
访问它。但是,当我尝试从
QWizard页面
访问数据时,它的父类型在代码执行期间从
QWizard
更改为
QForm
,因此我无法再以这种方式访问父数据。为什么会这样?我如何从任何
QWizard页面
可靠地访问存储在父
QWizard
中的数据

from PyQt4 import QtGui
from PyQt4.QtGui import QLabel, QVBoxLayout

class WizardExample(QtGui.QWizard):
    def __init__(self, parent=None):
        super(WizardExample, self).__init__(parent)
        self.setObjectName("WizardExample")
        self.setWindowTitle('Wizard example')
        #check data source once and store result
        self._dataExists = False
        self.addPage(Page1(self))

class Page1(QtGui.QWizardPage):
    def __init__( self, parent ):
        super(Page1, self).__init__(parent)
        layout = QVBoxLayout()
        self.setTitle('Page1')
        self.label = QLabel("Text entry... ")
        self.setObjectName("Page1")
        layout.addWidget(self.label)
        self.setLayout(layout)
        print("--__init__() parent()._dataExists: "+str(self.parent()._dataExists))
        self.printTypes()

    def printTypes(self):
        print(">>printTypes() self name: "+str(self.objectName())+" self type: "+str(type(self))+" parent name: "+self.parent().objectName()+" parent type: "+str(type(self.parent())))

    def isComplete(self): 
        print(">>isComplete()")
        self.printTypes()
        #fails on the following call
        if self.parent()._dataExists:
            return True
        else:
            return False

class AppWindow(QtGui.QMainWindow):
    def __init__( self, parent = None ):
        super(AppWindow, self).__init__(parent)

        wizard = WizardExample(self)
        wizard.exec_()


if ( __name__ == '__main__' ):
    app = None
    if ( not QtGui.QApplication.instance() ):
        app = QtGui.QApplication([])

    window = AppWindow()
    window.show()
    if ( app ):
        app.exec_()

由于在创建页面时将
QWizard
的引用传递到
QWizard页面
,因此您只需存储此引用并在以后使用即可(请参阅的注释)


或者,您可以使用
wizard()
方法(在的注释中提到)从
QWizard页面
中访问
QWizard
,该方法可能正是出于该原因(父级在内部更改)添加的.

如果将
self.parent=parent
添加到
Page1.\uuu init\uuuu
然后访问
self.parent.\u dataExists
而不是
self.parent()。\u dataExists
?@这是个好主意,虽然我仍然不了解根本原因,但我也不确定。这可能是因为赋值
self.parent=parent
会保留对QWizard对象的引用(否则可能会被废弃)。您还可以比较
print self.parent()之间的差异
在代码中的不同点打印self.parent
。此外,您还应该查看
设置字段
注册表字段
方法,因为它们提供了一种内置方式,可在向导页面之间共享数据,并可轻松用于验证输入数据。这可能会更干净,具体取决于您试图实现的目标。
QWizard
在页面内部使用
QFrame
。这就是为什么将页面添加到向导后,
.parent()
会发生更改
QWizardPage
有一个
.wizard()
方法,如果有向导,该方法将可靠地返回向导。