pyqt wizard registerfield as string and not integer

Question:

I am trying to create a Wizard using PyQt5 and I’m trying to make it so that the input entered by the user on one page influences what the user will see on the next page. However, when I try and do so, I only see the index value as opposed to the text.

Creating a list and then mapping the index value to the appropriate location in the list in order to get the string seems a bit janky, so does anyone know if PyQt has a better function built-in that does this?

class IntroductionPage(QtWidgets.QWizardPage):
    def __init__(self,*args,**kwargs):
        super().__init__()
        self.setTitle("Project Name")
        self.setSubTitle("Please select one of the available projects below")
        l = get_projects() 

        comboBox = QtWidgets.QComboBox(self)
        [comboBox.addItem(i) for i in l]
        layout = QtWidgets.QGridLayout()
        layout.addWidget(comboBox)
        self.setLayout(layout)
        self.registerField("projectName",comboBox)

class TaskPage(QtWidgets.QWizardPage):
    def __init__(self,*args,**kwargs):
        super().__init__()

    # currently returns an integer and not a string
    def initializePage(self):
        self.setTitle(self.field("projectName"))


class My_Wizard(QtWidgets.QWizard):
    num_of_pages = 2
    (intro,task) = range(num_of_pages)

    def __init__(self,*args,**kwargs):
        super(My_Wizard,self).__init__(*args,**kwargs)
        self.setPage(self.intro,IntroductionPage(self))
        self.setPage(self.task,TaskPage(self))
        self.setStartId(self.intro)
Asked By: kashmoney

||

Answers:

The registerField() method has more parameters:

void QWizardPage::registerField(const QString &name, QWidget *widget, const char *property = nullptr, const char *changedSignal = nullptr)

So the solution is to indicate that you want to get the currentText property of the QComboBox:

self.registerField("projectName", comboBox, "currentText")
Answered By: eyllanesc

been looking for this answer for a while, since the registerField() takes a property as a parameter.

in your wizard you can set a custom property for example if you were to use a table widget of some sort that doesn’t have get functionality built in to bulk get the whole table.

so you can do something like this


class SplitWizardPage(QWizardPage):
    def __init__(self, table_data, table_headers):
        super().__init__()

        self.table_data = table_data
        self.table_headers = table_headers

        self.table_widget = QTableWidget()
        self.table_widget.setProperty("mutated_table_data", [])

...

def on_cell_changed(self, row, column):
     ...some get data code
     self.table_widget.setProperty("mutated_table_data", table_data)

...

finally 

        self.table_widget.cellChanged.connect(lambda: on_cell_changed(self, row, column))

        self.registerField("split_table_data", self.table_widget, "mutated_table_data", self.table_widget.cellChanged)

finally.

wizard.finished.connect(lambda: print(wizard.field("split_table_data")))

as you can see we are attaching it to the property ‘mutated_table_data’ on the widget field

Answered By: Fanna1119