PyQT trouble passing filenames across QWizardPage

2019-08-21 09:20发布

I am trying to have the user upload a file to the application on one QWizardPage, and then be able to re-use that same file path on another QWizardPage. However, from my code

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

    def initializePage(self):
        self.setTitle("Choose file to execute")
        self.setSubTitle("Please choose a file to execute")
        self.myTextBox = QtWidgets.QTextEdit(self)
        self.myTextBox.setGeometry(QtCore.QRect(100, 0, 350, 50))

        self.uploader = QtWidgets.QPushButton("upload",self)
        self.uploader.clicked.connect(self.get_file_name)

    def get_file_name(self):
        self.name = QtWidgets.QFileDialog.getOpenFileName(self.uploader,'Choose File to Run')[0]
        self.registerField("file_name",self.name,"currentText")

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

    def initializePage(self):
        self.setSubTitle(self.field("file_name"))

I am getting an error

TypeError: registerField(self,str,QWidget,property: str = None, changedSignal: PYQT_SIGNAL = 0): argument 2 has unexpected type 'str'

Is there a simple way to convert this specific string (self.name) into a QWidget that is able to be passed onto other pages in the Wizard (i.e. in this example, onto the subtitle field in the Conclusion Page)?

I've looked through the documentation but can't figure it out so would appreciate a few pointers. Thanks.

1条回答
冷血范
2楼-- · 2019-08-21 10:03

You can only use registerField() method to pass a qproperty to the QWidget, in the case of QFileDialog it is not possible since there is no q-property associated with the selection also getOpenFileName() is a static method and getting the object is a complicated task, There are 2 possible solutions, the first is to create a class that inherits from QFileDialog and has a qproperty associated with the selection or use the magic of python to pass the values. The last one is the method I will use.:

class ExecutePage(QtWidgets.QWizardPage):
    ...
    def get_file_name(self):
        name, _ = QtWidgets.QFileDialog.getOpenFileName(self.uploader,'Choose File to Run')
        self.wizard().file_name = name

class ConclusionPage(QtWidgets.QWizardPage):
    ...
    def initializePage(self):
        if hasattr(self.wizard(), 'file_name'):
            self.setSubTitle(self.wizard().file_name)
查看更多
登录 后发表回答