【发布时间】:2022-11-28 23:50:34
【问题描述】:
我正在尝试创建一个非常简单的 QWizard(实际上是为不同错误创建最小可重现示例的过程的一部分)。我想要做的是访问 QWizardPage 的父级,即使用 .wizard() 调用。
这是代码:
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import sys
class MagicWizard(QWizard):
def __init__(self, parent=None):
super(MagicWizard, self).__init__(parent)
self.addPage(Page1(self))
self.setWindowTitle("PyQt5 Wizard Example - based on pythonspot.com example")
self.resize(640,480)
class Page1(QWizardPage):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.myLabel = QLabel("Testing registered fields")
layout = QVBoxLayout()
layout.addWidget(self.myLabel)
self.setLayout(layout)
print(self.wizard())
print(self.parent())
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
wizard = MagicWizard()
wizard.show()
sys.exit(app.exec())
这会正确加载并且控制台记录:
None
<__main__.MagicWizard object at 0x101693790>
第一行是对 self.wizard() 的调用,我希望它与 self.parent() 相同。我显然可以使用 .parent() 并且它会起作用,但我知道 .wizard() 是正确的方法。
【问题讨论】:
-
它显示
None,因为你在__init__中调用它,此时addPage()仍在等待构造函数返回实例。 -
谢谢!现在你提到它很明显。移动对 initializePage 函数的调用,我可以看到它有效。
标签: pyqt pyqt6 qwizard qwizardpage