QWizard with custom widgets

38 views Asked by At

I implemented a small widget to allow users to select a file and show the path in a QLineEdit:

class FileWidget(QWidget):
    fileChanged = pyqtSignal(str)
    filename = ""

    def __init__(self):
        super().__init__()

        self.setLayout(QHBoxLayout())

        self.__file_edit = QLineEdit()
        self.__file_edit.setEnabled(False)
        self.layout().addWidget(self.__file_edit)

        self.__file_button = QPushButton()
        self.__file_button.clicked.connect(self.__open_file_dlg)
        self.__file_button.setText("Select Template")
        self.layout().addWidget(self.__file_button)

    def __open_file_dlg(self, *args):
        selected_file = QFileDialog().getOpenFileName(self, "Select file", str(pathlib.Path.home().absolute()), "Documents (*.docx)")[0]
        self.__file_edit.setText(selected_file)
        self.filename = selected_file
        self.fileChanged.emit(selected_file)

Now, I'd like to use this widget in a QWizard. According to the documentation (https://doc.qt.io/qt-6/qwizard.html#setDefaultProperty) I need to specify the property which holds the value as well as a signal that is emitted, when the property changes:

wizard = QWizard()
wizard_page = QWizardPage()
wizard_page.setLayout(QFormLayout())

wizard.addPage(wizard_page)

widget = FileWidget()
wizard.setDefaultProperty(FileWidget.__name__, "filename", FileWidget.fileChanged)
wizard_page.layout().addRow("File Name:", widget)
wizard_page.registerField("filename*", widget)

wizard.show()
wizard.exec()

This displays everything correctly, however once I select a file, the QLineEdit in FileWidget receives the correct value, but it seems like the wizard is not notified.

I feel like I'm just missing one crucial step here. Would be glad if anybody shares their ideas.

0

There are 0 answers