4

I am trying to make a file picker to select a program (executable file).

My understanding of the documentation for QDir and QFileDialog is that the following should show a file dialog with only executable files. However, it shows no files at all (and no directories either) even though there are executable files in the directory.

    self.browseDialog = QtGui.QFileDialog()
    self.browseDialog.setFilter(QtCore.QDir.Executable | QtCore.QDir.Files)
    self.browseDialog.exec_()

Am I doing something wrong? Can this be achieved? I would also like to do something a little more complex: show files that are executable or end with certain extensions.

I am using PyQt 4.7.4 on Ubuntu Maverick.

Max
  • 1,620
  • 15
  • 22
  • Did you try or-ing in QtCore.QDir.AllDirs? – Michael Daum Apr 16 '12 at 16:58
  • 1
    Your example works fine for me and displays files. You can even remove the `Files` filter and just use `Executable`. Not sure why you are seeing different results. – jdi Apr 16 '12 at 17:13
  • @jdi, you **must** use the `Files` filter, too. The docs express this clearly: *The Executable value needs to be combined with Dirs or Files.* – Boštjan Mejak Feb 23 '22 at 23:28
  • But please be aware that if you're using one of the static methods (like `getOpenFileName()`), you must pass your filter as a string, and **not** as a flag. – Boštjan Mejak Feb 23 '22 at 23:54

1 Answers1

0

I am using PyQt5 but had to do a similar thing only I wanted just .txt files. To perform this I used getFileOpenName() and the filter= option. It worked fine for my purposes. The function names have changed from PyQt4 to PyQt5, but the link below has the names of the PyQt4 alternatives.

http://pyqt.sourceforge.net/Docs/PyQt5/pyqt4_differences.html

The getOpenFileNameAndFilter(), getOpenFileNamesAndFilter() and getSaveFileNameAndFilter() methods of PyQt4’s QFileDialog have now been renamed getOpenFileName(), getOpenFileNames() and getSaveFileName() respectively in PyQt5. PyQt4’s implementations of getOpenFileName(), getOpenFileNames() and getSaveFileName() are not supported in PyQt5.

10SecTom
  • 2,484
  • 4
  • 22
  • 26