0

As It's described in the docs initializePage should be called when the Wizard is started.

However in my code initializePage is not being called.

I notice that validatePage() is also not called, so I must be making a mistake.

Someone spots the error?

import sys
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QWizard, QWizardPage, QWidget

app = QtWidgets.QApplication([])

class MainWizard(QWizard):
    def __init__(self, parent=None):
        super().__init__(parent)
        uic.loadUi('inittest.ui', self)
        self.addPage(Page1(self))
        self.addPage(Page2(self))
        self.show()

class Page1(QWizardPage):
    def __init__(self, parent=None):
        super().__init__(parent)
        # instantiate the widgets
        self.label = parent.findChild(QtWidgets.QLabel, 'label')
        # self.initializePage()

    def initializePage(self) -> None:
        self.label.setText('Page1 initialized')

class Page2(QWizardPage):
    def __init__(self, parent=None):
        super().__init__(parent)
        # instantiate the widgets
        self.label_2 = parent.findChild(QtWidgets.QLabel, 'label_2')
        # self.initializePage()

    def initializePage(self) -> None:
        self.label_2.setText('Page2 initialized')

if __name__ == '__main__':
    main = MainWizard()
    main.show()
    sys.exit(app.exec_())

my ui file, inittest.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Wizard</class>
 <widget class="QWizard" name="Wizard">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Wizard</string>
  </property>
  <widget class="QWizardPage" name="Page1">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>80</y>
      <width>47</width>
      <height>14</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QWizardPage" name="Page2">
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>80</y>
      <width>47</width>
      <height>14</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

I expect the text of the label to be set. It doesn't

a34life
  • 1
  • 3
  • Your code does what asked (after removing the `uic` line): `validatePage()` is called, and it just crashes because it doesn't return a bool. Adding `initializePage()` to `SelectRoomPage` also works. So, you're probably doing something wrong in the UI file, or you never reach that page. Please provide a [mre]. Also note that you should not create the `app.exec()` outside of the `if __name__` block. – musicamante Mar 24 '23 at 16:13
  • I've changed my post to provide a minimal reproducible example. I hope its clear. If i uncomment the ```self.initializePage()``` in ```__init__```, it works. When commented the ```initializePage()``` is not called. – a34life Mar 24 '23 at 20:09
  • It *is* called. Just not when you think. In the UI you already have 2 existing pages, the two other pages you're adding by code are *appended* to those, and you're also changing the labels of the previous pages (which is conceptually wrong). Just press "Next" three times and "Previous" three times, you'll see the first label with the new text (partially shown because you didn't use [layout managers](//doc.qt.io/qt-5/layout.html)); the same if you press "Next" until you reach the end and go back to the second. – musicamante Mar 24 '23 at 22:18
  • If you want to add pages only by code, remove those pages in Designer and add labels (and proper layouts) to those QWizardPages subclasses. If you want to create the whole Wizard in Designer, then either use promoted widgets (look up for that even here in StackOverflow) for those pages with your subclasses, otherwise create QWizardPages in Designer and add them by code by loading their respective UI files. – musicamante Mar 24 '23 at 22:20
  • Thanks a lot for your answer. I understand what i was doing wrong. I'll give it a try creating the whole wizard in Designer. – a34life Mar 25 '23 at 08:52

0 Answers0