QFileDialog view folders and files but select folders only?

后端 未结 1 1449
广开言路
广开言路 2020-12-07 04:53

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

file_dialog = QtGui.QFileDialog()
file_dialog.setFileMode(QtGui.QFileDialog.Directory)
file         


        
相关标签:
1条回答
  • 2020-12-07 05:18

    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()
    
    0 讨论(0)
提交回复
热议问题