且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PyQT麻烦跨QWizardPage传递文件名

更新时间:2022-04-28 08:24:31

您只能使用registerField()方法将qproperty传递给QWidget,在QFileDialog的情况下,这是不可能的,因为没有与选择相关联的q属性也是getOpenFileName()的静态方法,获取对象是一项复杂的任务,有两种可能的解决方案,第一种是创建一个从QFileDialog继承并具有与qcproperty相关联的qproperty的类.选择或使用python的魔术来传递值.最后一个是我将使用的方法.

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)