How to use QFileDialog options and retrieve saveFileName?
Solution 1
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
Solution 2
dlg.selectedFiles()
returns a list of unicode strings containing the filenames selected.
Jesse
Updated on July 05, 2022Comments
-
Jesse almost 2 years
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.
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?
-
ekhumoro over 10 yearsDid you actually test any of this with pyqt4? Your example code produces a keyword error. But in any case, even with the right arguments, it won't give the output you claim it does (and the same goes for pyqt5 and pyside).
-
qurban over 10 yearsyes of course! I tested it and then pasted here. Works like a charm! After seeing your comment I again copied the above line and pasted it in eclipse, no problems at all!
-
ekhumoro over 10 yearsYour code seems to be Windows-specific: it doesn't work on Linux, and possibly doesn't on OSX (I can't test). By "doesn't work", I mean the extension is not appended (which is arguably the correct behaviour). The keyword error seems to be python3-specific, though - I don't get the error with python2. So, all-in-all, if you're looking for a general, cross-platform solution, the static methods aren't that great.
-
qurban over 10 yearsYes I am using Windows, but I will try it on Linux. Thank you for the useful information.
-
rbaleksandar over 8 yearsI dare disagree that there is no need to create a
QFileDialog
object.For example there is still an opened issue about the fact that the usage of the dialog doesn't automatically append the file extension based on the selected mime type filter so the developer has to take care of this him-/herself or otherwise things might get messy.PS: I also recommend not using "/path/to/file" but instead use theQDir
tools(such asQDir::homePath()
andQDir::cdUp()
(but notQDir::cd(...)
)) which provide at least partial handling of the problems that arise from using paths URLs on different platforms. -
limonik over 6 years@qurban I tried your code with windows platform and python 2.7 but the extension could not be appended.
-
Ciasto piekarz about 4 yearsdocstring in return type tells its a filename , they should instead be telling if its
string
or some other type.