PyQT trouble passing filenames across QWizardPage

巧了我就是萌 提交于 2019-12-11 23:38:31

问题


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:


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)


来源:https://stackoverflow.com/questions/51273301/pyqt-trouble-passing-filenames-across-qwizardpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!