QFileDialog view folders and files but select folders only?

自闭症网瘾萝莉.ら 提交于 2019-11-29 17:16:20

To prevent files being selected, you can install a proxy model which manipulates the flags for items 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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!