0

I've seen a couple of other posts (and another) trying to do what I'm asking here, but this is still not working. I have also been referencing this documentation and have checked help(QFileDialog.getSaveFileName) in the python console. Not sure if it has to do with the environment, or something my code is overlooking. I'm using Python 2.7 with PyQt 4.11.4 on Windows 7 64bit.

I've tried both direct object construction and the static methods (as suggested in this thread), but none of the following code allows me to set the default filter or initial filter for the file dialog. The file dialog just opens with the first file type (pdf) chosen, presumably because it's the first one on the list.

if self.dlg.formatBox.currentIndex() == 1 : # if extension is pdf
    dir = settings.value('/UI/lastSaveAsPdfFile')
else:
    dir = settings.value('/UI/lastSaveAsImageDir')

defaultFilter = self.tr(self.dlg.formatBox.currentText())
#defaultFilter = "TIF format (*.tif *.TIF)"

#(folderDialog, selectedFilter) = QFileDialog.getSaveFileNameAndFilter(
    #None,
    #'Impression',
    #dir,
    #"PDF format (*.pdf *.PDF);;JPG format (*.jpg *.JPG);;JPEG format (*.jpeg *.JPEG)');;TIF format (*.tif *.TIF);;TIFF format (*.tiff *.TIFF);;PNG format (*.png *.PNG);;BMP format (*.bmp *.BMP);;ICO format (*.ico *.ICO);;PPM format (*.ppm *.PPM);;XBM format (*.xbm *.XBM);;XPM format (*.xpm *.XPM)",
    #defaultFilter
    #)

#folderDialog = QFileDialog.getSaveFileName(
    #None,
    #'Impression',
    #dir,
    #"PDF format (*.pdf *.PDF);;JPG format (*.jpg *.JPG);;JPEG format (*.jpeg *.JPEG)');;TIF format (*.tif *.TIF);;TIFF format (*.tiff *.TIFF);;PNG format (*.png *.PNG);;BMP format (*.bmp *.BMP);;ICO format (*.ico *.ICO);;PPM format (*.ppm *.PPM);;XBM format (*.xbm *.XBM);;XPM format (*.xpm *.XPM)",
    #defaultFilter
    #)

folderDialog = QFileDialog(None, 'Impression', dir, "PDF format (*.pdf *.PDF);;JPG format (*.jpg *.JPG);;JPEG format (*.jpeg *.JPEG)');;TIF format (*.tif *.TIF);;TIFF format (*.tiff *.TIFF);;PNG format (*.png *.PNG);;BMP format (*.bmp *.BMP);;ICO format (*.ico *.ICO);;PPM format (*.ppm *.PPM);;XBM format (*.xbm *.XBM);;XPM format (*.xpm *.XPM)")
folderDialog.selectNameFilter(defaultFilter)
folderDialog.exec_()

print 'Default Filter: {}'.format(defaultFilter)

self.dlg.formatBox is a combo box in the parent window where the user can choose his desired format. I would like to retrieve this information and set the default filter to this format when opening the file dialog. Running the code in the way it is presented, with the 6th filter option (png) selected (in self.dlg.formatBox) gives me the following dialog--still pdf:

enter image description here

The following is printed to the console:

Default Filter : Format PNG (*.png *.PNG)

UPDATE defaultFilter variable has been modified to take into consideration question comments.

Community
  • 1
  • 1
user25976
  • 1,005
  • 4
  • 18
  • 39
  • The `defaultFilter` is not in any of your lists of filters, so obviously it cannot be pre-selected. As stated in the first linked answer, the `QFileDialog` constructor example **does** work correctly. However, the static methods do not seem to work properly in either PyQt4 or PySide. All the methods work okay in PyQt5. – ekhumoro Dec 20 '16 at 22:29
  • @ekhumoro Okay, so focusing on the direct construction method--can you see where it is still going wrong? I've edited a bit the question – user25976 Dec 20 '16 at 22:57
  • @ekhumoro also that defaultFilter value was a poorly thought out test to see if my issue was formatting. The other defaultFilter values (commented out) don't work either – user25976 Dec 20 '16 at 23:30
  • Please check your tests more carefully - none of your `defaultFilter` strings are in any of your filter lists. I suggest you copy one of the actual filters in the list to use for testing. – ekhumoro Dec 20 '16 at 23:54
  • @ekhumoro self.tr(self.dlg.formatBox.currentText()) is in the filter list, as the formatBox contains filter list items. Apologies for the typos, I'm re-typing some of this stuff out--no internet on the coding computer...You think it may indeed be a formatting problem? – user25976 Dec 21 '16 at 00:48
  • Is the code in the question what you are actually running? You say that "Format PNG (*.png *.PNG)" is printed to the console, but that is not in the list of filters that is shown in the code. Please read the guidance on how to produce a [mcve]. – ekhumoro Dec 21 '16 at 02:19
  • @ekhumoro okay...i finally see what you mean. And I just realized that there's some sort of french language translation going on that I didn't realize was happening...!!what I was seeing as "PNG Format..." was getting translated to "Format PNG..." – user25976 Dec 21 '16 at 02:40

2 Answers2

0

The static method options don't seem to be any option in my current environment (PyQt 4.11 with Python 2.7)--apparently they are broken.

The direct object construction is the way to go. This didn't initially work, because the default filter did not match the file dialog set filters--these need to match exactly or else the file dialog won't be able to set the default filter.

user25976
  • 1,005
  • 4
  • 18
  • 39
0

I used this method, This way you can add as many extension types to the DialogBox in pyqt5.

filenames, _ = QFileDialog.getOpenFileNames(caption='Open a file',
                                            directory='',
                                            filter='Python Files (*.py)\nText Files (*.txt)\n'
                                                   'CSS Files (*.css)\nJSON File (*.json)',
                                            options=options)
Rushi99
  • 1
  • 1