PyQT麻烦跨QWizardPage传递文件名 [英] PyQT trouble passing filenames across QWizardPage

查看:301
本文介绍了PyQT麻烦跨QWizardPage传递文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我遇到错误

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

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

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

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.

推荐答案

您只能使用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)

这篇关于PyQT麻烦跨QWizardPage传递文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆