如何在qlistview python中过滤文件

时间:2018-11-25 23:48:20

标签: python python-3.x pyqt pyqt5 qfilesystemmodel

我正在尝试从用户选择的下拉选项中过滤列表视图中的元素。到目前为止,这是我的代码。

class DirectoryView(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QHBoxLayout(self)
        self.listview = QListView()
        self.layout.addWidget(self.listview)
        self.setAcceptDrops(True)
        self.listview.setSelectionMode(
            QtWidgets.QAbstractItemView.ExtendedSelection
        )
        self.fileModel = QFileSystemModel()
        self.listview.setModel(self.fileModel)
        self.cb = QComboBox()
        self.layout.addWidget(self.cb)
        self.cb.currentTextChanged.connect(self.filterClicked)
        self.cb.addItem(".mp4")
        self.cb.addItem(".gif")

    def dragEnterEvent(self, e):
        e.accept()

    def dropEvent(self, e):
        if e.mimeData().hasUrls():
            e.accept()
            for url in e.mimeData().urls():
                print(url)
                fname = str(url.toLocalFile())
        self.updateDirectoryView(fname)

    def updateDirectoryView(self,path):
        self.listview.setRootIndex(self.fileModel.setRootPath(path))

    def filterClicked(self):
        print("todo")

我想在下拉菜单的用户更改选项时过滤元素。

1 个答案:

答案 0 :(得分:0)

您必须使用setNameFilters()并传递通配符列表,此外还要将False设置为nameFilterDisables

from PyQt5 import QtCore, QtGui, QtWidgets

class DirectoryView(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)

        self.listview = QtWidgets.QListView()
        self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.fileModel = QtWidgets.QFileSystemModel(nameFilterDisables=False)
        self.listview.setModel(self.fileModel)

        self.cb = QtWidgets.QComboBox()
        self.cb.currentTextChanged.connect(self.filterChanged)

        layout = QtWidgets.QHBoxLayout(self)
        layout.addWidget(self.listview)
        layout.addWidget(self.cb)

    def dragEnterEvent(self, e):
        e.accept()

    def dropEvent(self, e):
        if e.mimeData().hasUrls():
            e.accept()
            for url in e.mimeData().urls():
                if url.isLocalFile():
                    if self.updateDirectoryView(url.toLocalFile()):
                        break

    def updateDirectoryView(self, path):
        fi = QtCore.QFileInfo(path)
        if fi.isDir():
            self.listview.setRootIndex(self.fileModel.setRootPath(path))
            d = QtCore.QDir(path)
            suffixes = set()
            for fi in d.entryInfoList(filters=QtCore.QDir.Files):
                if fi.isFile():
                    suffixes.add("."+fi.suffix())
            self.cb.clear()
            self.cb.addItems(sorted(suffixes))
            return True
        return False

    @QtCore.pyqtSlot(str)
    def filterChanged(self, text):
        self.fileModel.setNameFilters(["*"+text])

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = DirectoryView()
    w.show()
    sys.exit(app.exec_())