Python 如何使用 QFileDialog 选项并检索 saveFileName?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20928023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:34:31  来源:igfitidea点击:

How to use QFileDialog options and retrieve saveFileName?

pythonpyqtpyqt4execqfiledialog

提问by user3161430

I'm trying to use a QFileDialog to prompt a user to provide a filename and location to save a text file at. I played around with the QtGui.QFileDialog.getSaveFileName, but I was interested in using some of the options, like setting the default suffix, and enabling the Detail view of the save file dialog, which, from what I could tell, isn't possible to do, using the getSaveFileName alone. Whenever I set those, the getSaveFileName dialog just ignored them.

我正在尝试使用 QFileDialog 提示用户提供文件名和位置以保存文本文件。我尝试了 QtGui.QFileDialog.getSaveFileName,但我对使用一些选项感兴趣,例如设置默认后缀,并启用保存文件对话框的详细信息视图,据我所知,这不是可以做到,单独使用 getSaveFileName。每当我设置这些时,getSaveFileName 对话框就会忽略它们。

So, I ended up with something like this:

所以,我最终得到了这样的结果:

dlg=QtGui.QFileDialog( self )
dlg.setWindowTitle( 'Print Things' )
dlg.setViewMode( QtGui.QFileDialog.Detail )
dlg.setNameFilters( [self.tr('Text Files (*.txt)'), self.tr('All Files (*)')] )
dlg.setDefaultSuffix( '.txt' )
if dlg.exec_() :
    print dlg

However, now I'm not sure how to get the name of the file passed by the user? If I print dlg.getSaveFileName, it just pops up another save file dialog. Anybody know how to do this, while still passing all of the options to the QFileDialog that I want to be respected?

但是,现在我不确定如何获取用户传递的文件名?如果我打印 dlg.getSaveFileName,它只会弹出另一个保存文件对话框。任何人都知道如何做到这一点,同时仍然将所有选项传递给我希望受到尊重的 QFileDialog ?

回答by three_pineapples

dlg.selectedFiles()returns a list of unicode strings containing the filenames selected.

dlg.selectedFiles()返回包含所选文件名的 unicode 字符串列表。

回答by qurban

There is no need to create object of QFileDialogbecause it provides four static methods which can be used according to your needs.

无需创建对象,QFileDialog因为它提供了四种静态方法,可以根据您的需要使用。

1) QFileDialog.getExistingDirectory(...)
2) QFileDialog.getOpenFileName(...)
3) QFileDialog.getOpenFileNames(...)
4) QFileDialog.getSaveFileName(...)

according to your needs, you need the 4th one. You can also provide arguments to this function for default file extension. You can use it as:

根据您的需要,您需要第四个。您还可以为此函数提供参数以获取默认文件扩展名。您可以将其用作:

fileName = QtGui.QFileDialog.getSaveFileName(self, 'Dialog Title', '/path/to/default/directory', selectedFilter='*.txt')
if fileName:
    print fileName

You can leave the /path/to/default/directoryas empty string if you don't have any clue that in which directory a user can save the file.

/path/to/default/directory如果您不知道用户可以将文件保存在哪个目录中,您可以将其保留 为空字符串。

Now when user clicks the save button on the dialog after putting a file name (without file extension), this method will return the file path followed by .txtextension.

现在,当用户在输入文件名(不带文件扩展名)后单击对话框上的保存按钮时,此方法将返回后跟.txt扩展名的文件路径。

More information about QFileDialog.getSaveFileName()can be found here

QFileDialog.getSaveFileName()可以在此处找到有关的更多信息