12

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?

answerSeeker
  • 2,256
  • 4
  • 24
  • 60
user3161430
  • 145
  • 1
  • 1
  • 9

2 Answers2

21

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

qurban
  • 3,529
  • 18
  • 33
  • Did 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). – ekhumoro Jan 05 '14 at 20:13
  • yes 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! – qurban Jan 06 '14 at 14:53
  • Your 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. – ekhumoro Jan 06 '14 at 18:37
  • Yes I am using Windows, but I will try it on Linux. Thank you for the useful information. – qurban Jan 07 '14 at 15:38
  • I 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 the `QDir` tools(such as `QDir::homePath()` and `QDir::cdUp()`(but not `QDir::cd(...)`)) which provide at least partial handling of the problems that arise from using paths URLs on different platforms. – rbaleksandar Mar 14 '16 at 18:46
  • @qurban I tried your code with windows platform and python 2.7 but the extension could not be appended. – limonik Jan 03 '18 at 15:35
  • docstring in return type tells its a filename , they should instead be telling if its `string` or some other type. – Ciasto piekarz Jun 17 '20 at 17:01
5

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

three_pineapples
  • 10,414
  • 4
  • 35
  • 64