0

I'm trying to create a very simple QWizard (actually as part of the process to create a min reproducible example for a different error). What I want to be able to do is to access the QWizardPage's parent, i.e. using the .wizard() call.

Here is the code:

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

This loads correctly and the console logs:

None
<__main__.MagicWizard object at 0x101693790>

The first line is the call to self.wizard() which I was expecting would be the same as self.parent(). I can obviously use .parent() and it will work but I understood that .wizard() was the correct way to go.

peetysmith
  • 157
  • 1
  • 10
  • It shows `None` because you're calling it in the `__init__`, and at the moment `addPage()` is still waiting for the constructor to return the instance. – musicamante Nov 28 '22 at 14:24
  • Thank you! Obvious now that you mention it. Moving the call to initializePage function I can see it works. – peetysmith Nov 28 '22 at 15:32

1 Answers1

0

As per guidance from @musicamante I've changed to move the wizard() call out of the constructor where it (obviously) will not work. It now looks like this and works fine.

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)
    
    def initializePage(self):
        print(self.wizard())

    def button_push(self):
        print(self.wizard())

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    wizard = MagicWizard()
    wizard.show()
    sys.exit(app.exec())
peetysmith
  • 157
  • 1
  • 10