QFileDialog view folders and files, but only select folders?

I create my own dialog with the file 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 behavior that interests me is that the user can view files and folders, but only select folders. (making files inaccessible). Is it possible?

Note: Using the DirectoryOnly parameter is not suitable for me, because it does not allow viewing files, just folders.

Edit (additional code that I forgot to add, which is responsible for the ability to select multiple folders instead of 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) 
+1
source share
1 answer

To prevent the selection of files, you can install a proxy model that controls the flags for elements in the file view:

 class ProxyModel(QtGui.QIdentityProxyModel): def flags(self, index): flags = super(ProxyModel, self).flags(index) if not self.sourceModel().isDir(index): flags &= ~QtCore.Qt.ItemIsSelectable return flags # keep a reference somewhere to prevent core-dumps on exit self._proxy = ProxyModel(self) file_dialog.setProxyModel(self._proxy) 
+2
source

Source: https://habr.com/ru/post/1209308/


All Articles