简体   繁体   English

PyQt4:使用QFileDialog()保存文件而不是QFileDialog()。getSaveFileName()

[英]PyQt4: Using QFileDialog() to save files not QFileDialog().getSaveFileName()

I am trying to create a program that allows the user to open a pre-existing file and save current files. 我正在尝试创建一个程序,该程序允许用户打开一个预先存在的文件并保存当前文件。 For opening a file I am using: 为了打开文件,我正在使用:

dlg = QFileDialog(self, "Open", "", "Yaml(*.yaml)")
        filenames = QStringList()

        if dlg.exec_():
            filenames = dlg.selectedFiles()
            FILE_NAME = str(QFileInfo(filenames[0]).baseName())

For saving files I am using: 为了保存文件,我正在使用:

_fileName = QFileDialog().getSaveFileName(self, "Save", "./", "Yaml(*.yaml)")
    FILE_NAME = str(QFileInfo(_fileName).baseName())

However, graphically I am noticing differences between the open and save methods. 但是,在图形上,我注意到open和save方法之间的差异。

I know I am not using QFileDialog.getOpenFileName(...) This is because QFileDialog.getSaveFileName(...) outputs a bunch of errors when loading the GUI. 我知道我没有使用QFileDialog.getOpenFileName(...),这是因为QFileDialog.getSaveFileName(...)在加载GUI时会输出一堆错误。

Failed enumerating UDisks2 objects: "org.freedesktop.DBus.Error.Disconnected" 
"Not connected to D-Bus server" 

Is there anyway that I can use QFileDialog to save files? 无论如何,我可以使用QFileDialog保存文件吗? Note that QFileDialog() by default has an "Open" button, is there anyway to change this to "Save" 请注意,默认情况下,QFileDialog()具有一个“打开”按钮,是否可以将其更改为“保存”

I found a solution. 我找到了解决方案。

QFileDialog has a method called setAcceptMode(QFileDialog.AcceptMode) which allows you to change between Open and Save. QFileDialog有一个名为setAcceptMode(QFileDialog.AcceptMode)的方法,该方法使您可以在“打开”和“保存”之间进行切换。 http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#setAcceptMode http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#setAcceptMode

Usage for open: 开放使用:

QFileDialog.setAcceptMode(QtGui.QFileDialog.AcceptOpen)

Usage for Save: 保存用法:

QFileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM