PDA

View Full Version : PyQT5 QFileDialog only works correctly when the file mode is set to 'Directory'



randizzleDee
25th July 2019, 17:17
The following code works as expected:


dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.Directory)
if dialog.exec():
save_folder = dialog.selectedFiles()

However, changing the file mode and adding a filter causes the exec call to fail:



image_dialog = QFileDialog(self)
image_dialog.setFileMode(QFileDialog.ExistingFiles )
image_dialog.setNameFilter("Images (*.png *.jpg *.pgm)")
if image_dialog.exec():
filename = image_dialog.selectedFiles()


Any Idea why this might be the case? Both code snippets reside in the same slot function within the main window class, and this behavior seems to be consistent no matter where In the program I move them to.

anda_skoa
26th July 2019, 08:05
That looks like it should work.

Have you tried narrowing it down to either the filter or the different mode?

Also you might want to check if the Python API offers the same static convenience functions the C++ API has.
QFileDialog::getOpenFileName() is much more straight forward to use.

Cheers,
_