0

我试图让用户将文件上传到一个 QWizardPage 上的应用程序,然后能够在另一个 QWizardPage 上重新使用相同的文件路径。但是,从我的代码

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"))

我收到一个错误

TypeError:registerField(self,str,QWidget,property:str = None,changedSignal:PYQT_SIGNAL = 0):参数2具有意外类型'str'

有没有一种简单的方法可以将此特定字符串(self.name)转换为能够传递到向导中其他页面(即在本例中,传递到结论页面中的字幕字段)的 QWidget?

我已经查看了文档,但无法弄清楚,所以希望得到一些指导。谢谢。

4

1 回答 1

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)
于 2018-07-10T20:29:09.730 回答