M
M
Mikhail Kuligin2017-01-05 22:33:17
Python
Mikhail Kuligin, 2017-01-05 22:33:17

How to pass the value of the registerField field from one page to another in PyQt5 in a QWizard multi-page wizard?

PyQt has a QWizard class for creating a multi-page wizard
The child (QWizard) QWizardPage class has a method registerField(<Property>, [, property=None][,changedSignal=0]) which, as stated in the documentation, "registers a property with which you can access the value of the component from any page of the wizard"
but you can't access the value of page2_number_of_factors on the 3rd page of the
string wizard

print('print from page 3 - page2_number_of_factors = ', self.field('page2_number_of_factors'))
        self.print_parameter_from_page2()

    def print_parameter_from_page2(self):
        path = self.field("page2_number_of_factors")
        print("page2_number_of_factors = ", path)

they output None
and I need access to the values , but in the
on_click () method, the line
print(wizard.field('page2_number_of_factors'))
works fine . the necessary methods of 3 classes have a common parent QWizardPage in the function an object is created wizard = Wizard(window) then with its help the data saved from 3 classes is obtained print(wizard.field('page2_number_of_factors')) how to access these methods from inside other classes?
Now the 3rd page of the wizard is just a stub, I need to dynamically generate it depending on the value of page2_number_of_factors
import configparser
from configparser import ConfigParser
from PyQt5 import QtCore
from PyQt5 import QtWidgets

class Page1(QtWidgets.QWizardPage):
    def __init__(self, parent=None):
        QtWidgets.QWizardPage.__init__(self, parent)
        self.setTitle("Select project name")
        self.label_page1 = QtWidgets.QLabel("Select folder, project file name")
        self.button_page1 = QtWidgets.QPushButton("Browse")
        self.line_edit_page1 = QtWidgets.QLineEdit()
        self.box = QtWidgets.QVBoxLayout()
        self.box.addWidget(self.label_page1)
        self.box.addWidget(self.line_edit_page1)
        self.box.addWidget(self.button_page1)
        self.setLayout(self.box)
        self.registerField("page1data*", self.line_edit_page1)
        self.button_page1.clicked.connect(self.save_file_project)
        
    # Open dialog 'Save Project file'
    def save_file_project(self):
        # global PROJECT_FILE_NAME
        f, l = QtWidgets.QFileDialog.getSaveFileName(parent=None,
                                                     caption='Save file Project',
                                                     directory=QtCore.QDir.currentPath(),
                                                     filter="All (*);;Projects (*.pydoe)",
                                                     initialFilter="Projects (*.pydoe)")

        print('File Name:', f)
        file = open(f, 'w')
        print('File', ' ', file, ' ', 'opened')

        self.line_edit_page1.setText(f)
        config = configparser.RawConfigParser()
        print('ConfigParser.RawConfigParser()')
        config.add_section('DOE_parameters')        
        config.set('DOE_parameters', 'PROJECT_FILE_NAME', f)       
        # with open(f, 'wb') as configfile:
        config.write(file)
        print('on_clicked.config.write')
        file.close() 

class Page2(QtWidgets.QWizardPage):
    def __init__(self, parent=None):
        QtWidgets.QWizardPage.__init__(self, parent)        
        self.setTitle("Select parameters of experiment")
        self.type_of_experiments = QtWidgets.QComboBox()
        self.number_of_factors = QtWidgets.QComboBox()
        self.box_page2 = QtWidgets.QVBoxLayout()
        # Create list 'type of experiments'        
        self.box_page2.addWidget(self.type_of_experiments)
        self.type_of_experiments.addItems(["Select type of experiment",
                                          "Full factorial experiment",
                                          "Rotatable plan (second order)"])
        print('block 2 pass')
        # Create list 'number of factors'
        self.box_page2.addWidget(self.number_of_factors)
        self.number_of_factors.addItems(["Select number of factors",
                                         "2",
                                         "3",
                                         "4",
                                         "5",
                                         "6",
                                         "7"])        
        self.setLayout(self.box_page2)
        self.registerField('page2_type_experiment', self.type_of_experiments)
        self.registerField('page2_number_of_factors', self.number_of_factors)
        print('page2_number_of_factors', self.field('page2_number_of_factors'))

class Page3(QtWidgets.QWizardPage):
    def __init__(self, parent=None):
        QtWidgets.QWizardPage.__init__(self, parent)
        self.setTitle("Название страницы 3")
        self.setSubTitle("Текст подзаголовка")
        self.label3 = QtWidgets.QLabel("Содержимое страницы 3")
        self.line3 = QtWidgets.QLineEdit()
        self.box3 = QtWidgets.QVBoxLayout()
        self.box3.addWidget(self.label3)
        self.box3.addWidget(self.line3)
        self.setLayout(self.box3)
        self.registerField("page3data*", self.line3)
        print('print from page 3 - page2_number_of_factors = ', self.field('page2_number_of_factors'))
        self.print_parameter_from_page2()

    def print_parameter_from_page2(self):
        path = self.field("page2_number_of_factors")
        print("page2_number_of_factors = ", path)
        
class Wizard(QtWidgets.QWizard):
    def __init__(self, parent=None):
        QtWidgets.QWizard.__init__(self, parent)

        self.setWindowTitle("Create New Project")
        self.setWizardStyle(QtWidgets.QWizard.ClassicStyle)
        self.page1 = Page1()
        self.page2 = Page2()
        self.page3 = Page3()
        self.idPage1 = self.addPage(self.page1)
        self.idPage2 = self.addPage(self.page2)
        self.idPage3 = self.addPage(self.page3)


def on_clicked(self):
    print('start onclick')
    # app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()
    print('window = QtWidgets.QWidget()')
    window.setWindowTitle("New Project QWizard")
    window.resize(300, 70)
    window.show()
    wizard = Wizard(window)
    result = wizard.exec()

    if result == QtWidgets.QDialog.Accepted:
        print("Нажата кнопка Finish")
        print(wizard.field("page1data"))
        print(wizard.field('page2_type_experiment'))
        print(wizard.field('page2_number_of_factors'))        
        print(wizard.field("page3data"))
    else:
        print("Нажата кнопка Cancel, кнопка Закрыть или клавиша <Esc>", result)

UPD
it turns out that in order for access to field() to work, you need to override the initializePage() method in the wizard page class where variables from other pages will be used
initializePage(self) - This method should be overridden in a class that inherits
the QWizardPage class if you want to set component properties based on data entered on previous pages. The method is called when
the Next button on the previous page is clicked, but before the next page is displayed.

def initializePage(self):
        path = self.field("line1")
        print('print from function = ', path)

an example of a working code on overflow , since I can’t insert here due to the character limit

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question