gpt4 book ai didi

python - 如何使用QFileSystemModel过滤多个目录并显示在QTreeView上?

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:20 29 4
gpt4 key购买 nike

如何使用 QFileSystemModel 过滤 PyQt4 中的多个目录并将它们显示在 QTreeView 上?

我努力

  1. 子类QFileSystemModel,但我不知道如何返回rowCount

  2. 使用QSortFilterProxyModel -> filterAcceptsRow(),但是很难返回

  3. QFileSystemWatcher 不好。

也许我没有做正确的事。

我是否应该通过win32监控目录并创建自己的模型和节点?

例如1 enter image description here

例如2 enter image description here


#!/usr/bin/python

import sys
from functools import partial
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtGui
from PyQt4 import QtCore


disk = 'C:/'
dir_1 = 'C:/Python27'
dir_1_1 = 'C:/Python27/Lib'
dir_1_1_1 = 'C:/Python27/Lib/bsddb'


class FilterProxyModel(QSortFilterProxyModel):
def __init__(self):
super(FilterProxyModel, self).__init__()
self.count = 0

def filterAcceptsRow(self, src_row, src_parent):

src_model = self.sourceModel()
src_index = src_model.index(src_row, 0, src_parent)

item_data = src_model.itemData(src_parent)
# print 'item_data: ', item_data
item_var = src_index.data(Qt.DisplayRole)
# print 'item_var: ', item_var

file_path = src_model.filePath(src_index)
file_path = str(file_path)
if disk in file_path:
# print 'file_path: ', file_path
if file_path == disk:
# print 'file_path: ', file_path
return True

elif dir_1 == file_path:
# print 'file_path: ', file_path
return True

elif dir_1_1 == file_path:
# print 'file_path: ', file_path
return True

elif dir_1_1_1 == file_path:
# print 'file_path: ', file_path
return True

elif file_path.endswith('.py'):
print 'file_path: ', file_path
return True

return False



class MyQFileSystemModel(QFileSystemModel):
"""docstring for MyQFileSystemModel"""
def __init__(self, parent=None):
super(MyQFileSystemModel, self).__init__(parent)

def columnCount(self, parent):
return 1


class MyWindow(QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)

self.pathRoot = QDir.rootPath()

# self.model = QFileSystemModel(self)
self.model = MyQFileSystemModel(self)
self.model.setRootPath(self.pathRoot)
self.model.setNameFilterDisables(0)


filter = ['*.py', '*mll']
self.model.setNameFilters(filter)
self.model.setNameFilterDisables(0)
# self.model.removeColumn(2)

self.proxy = FilterProxyModel()
self.proxy.setSourceModel(self.model)
self.proxy.setDynamicSortFilter(True)

self.treeView = QTreeView(self)
self.treeView.setModel(self.proxy)
# self.treeView.setRootIndex(self.indexRoot)
# self.listView = QListView(self)
# self.listView.setModel(self.proxy)


self.layout = QHBoxLayout(self)
self.layout.addWidget(self.treeView)
# self.layout.addWidget(self.listView)



if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName('MyWindow')

main = MyWindow()
main.resize(666, 333)

main.show()

sys.exit(app.exec_())

我就是这样做的。最后,我放弃了,我用另一种方法。

最佳答案

我正是在你的情况下,发现了一些东西。诀窍是获取要隐藏的路径列表,然后使用 setHiddenRow 方法将其传递给 treeView。这是我在addWidget方法之前写的

# hide all no desired folders.
foldersToShow = []
for currentDir, dirnames, filenames in os.walk(rootPath):
if currentDir in "/folder/I/want/to/show" : #whatever condition you prefer, this is your main filter for what you want to KEEP
foldersToShow.append(currentDir)
else:
index = self.model.index(currentDir)
self.treeView.setRowHidden(index.row(), index.parent(), True) # what doesnt meet your requirements get to be hidden

从这里开始,有一个问题。由于我不明白的原因,您要保留的文件夹中的文件未显示。但是有一种方法可以显示它们。从每个目录中,将每个文件的 rowHidden 参数设置为 false

# display all files from foldersToShow
for folder in foldersToShow:
for currentDir, dirnames, filenames in os.walk(folder):
for filename in filenames:
filenamePath = os.path.join(currentDir.replace(r"/",'\\'), filename)
fileId = self.model.index(filenamePath)
self.treeView.setRowHidden(fileId.row(), fileId.parent(), False)

layout.addWidget(self.treeView)

关于python - 如何使用QFileSystemModel过滤多个目录并显示在QTreeView上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47266719/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com