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:
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.