How to use QFileDialog options and retrieve saveFileName?

半腔热情 提交于 2019-12-03 02:53:19

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

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/directory as empty string if you don't have any clue that in which directory a user can save the file.

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 .txt extension.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!