2

I'm creating my own custom file dialog using the following code:

file_dialog = QtGui.QFileDialog()
file_dialog.setFileMode(QtGui.QFileDialog.Directory)
file_dialog.setViewMode(QtGui.QFileDialog.Detail)
file_dialog.setOption(QtGui.QFileDialog.DontUseNativeDialog, True)

The behaviour that i'm interested in is for the user to be able to view both files and folders, but select folders only. (making files unselectable). Is that possible?

Note: Using the DirectoryOnly option is not good for me since it doesn't allow you to view files, just folders.

Edit (extra code that i forgot to add which responsible for being able to select multiple folders instead of just one):

file_view = file_dialog.findChild(QtGui.QListView, 'listView')
if file_view:
    file_view.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
f_tree_view = file_dialog.findChild(QtGui.QTreeView)
if f_tree_view:
    f_tree_view.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
YaronGh
  • 371
  • 2
  • 4
  • 13
  • The code you've posted does exactly what you say you want (i.e. the user can view files and dirs, but only choose dirs). So maybe you need to explain more clearly what it is you want to achieve. – ekhumoro Jul 27 '16 at 23:02
  • Not really.. the code above allows you to choose files as well. – YaronGh Jul 28 '16 at 13:36
  • For me, the "Choose" button only becomes enabled when I select directories. It is possible to *select* files, but not choose them (because the button is disabled). – ekhumoro Jul 28 '16 at 17:08
  • this is a bit awkward but i just discovered that there was another piece of code that was responsible for it. it's a code that allows me to select multiple folders instead of just one - but then it does so while also giving me the problem i mentioned. i edited my original post with that code. – YaronGh Jul 28 '16 at 17:18
  • see my last comment;) – YaronGh Jul 28 '16 at 17:20

1 Answers1

3

To prevent files being selected, you can install a proxy model which manipulates the flags for items in the file-view:

dialog = QFileDialog()
dialog.setFileMode(QFileDialog.Directory)
dialog.setOption(QFileDialog.DontUseNativeDialog, True)

class ProxyModel(QIdentityProxyModel):
    def flags(self, index):
        flags = super(ProxyModel, self).flags(index)
        if not self.sourceModel().isDir(index):
            flags &= ~Qt.ItemIsSelectable
            # or disable all files
            # flags &= ~Qt.ItemIsEnabled
        return flags

proxy = ProxyModel(dialog)
dialog.setProxyModel(proxy)

dialog.exec()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Okay great thanks! I'm not sure if i should open a new question for that or not (let me know) - but is there a quick modification i could make to my code above to be able to select both folders and files? – YaronGh Jul 28 '16 at 18:59
  • 1
    @YaronGh. `file_dialog.setProxyModel(None)`. – ekhumoro Jul 28 '16 at 19:02
  • If i do that it returns the selected file + all of the other files in that folder – YaronGh Jul 28 '16 at 19:07
  • @YaronGh. You're going to have to explain much more clearly what you're trying to do. – ekhumoro Jul 28 '16 at 19:12
  • I'm trying to select both files and folders, and have the file dialog returns the paths to those files and folders. – YaronGh Jul 28 '16 at 19:15
  • @YaronGh. That sounds like a [completely different question](http://stackoverflow.com/q/27005858/984421). – ekhumoro Jul 28 '16 at 19:40