您好,我正在尝试在一个中组织多个路径
treeview
,但此代码不起作用
我收到如下错误:
AttributeError: 'QStandardItem' object has no attribute 'findItems'
its like QStandardItem do no have a function to find the item
你有什么想法吗
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QTreeView, QVBoxLayout, QWidget
class TreeViewExample(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QTreeView Example")
self.setGeometry(100, 100, 800, 600)
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.tree_view = QTreeView(self.central_widget)
self.model = QtGui.QStandardItemModel()
self.tree_view.setModel(self.model)
# Replace this list with your own list of file paths
file_paths = [
"/home/user/documents/file1.txt",
"/home/user/documents/folder1/file2.txt",
"/home/user/documents/folder1/file3.txt",
"/home/user/pictures/photo1.jpg",
"/home/user/pictures/folder2/photo2.jpg",
]
# Organize file paths in parent-child hierarchy
self.organize_paths(file_paths)
# Set up the layout
layout = QVBoxLayout(self.central_widget)
layout.addWidget(self.tree_view)
def organize_paths(self, file_paths):
for path in file_paths:
components = path.split("/")
current_parent = None
for component in components:
print(component)
item = self.get_or_create_item(component, parent=current_parent)
current_parent = item
def get_or_create_item(self, text, parent=None):
items = parent.findItems(text) if parent else self.model.findItems(text)
if items:
return items[0]
else:
item = QtGui.QStandardItem(text)
if parent:
parent.appendRow(item)
else:
self.model.appendRow(item)
return item
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TreeViewExample()
window.show()
sys.exit(app.exec_())
QStandardItem
没有 findItems()
,但您可以使用 for
循环与 .rowCount()
和 .child(index)
来检查 child.text()
def get_or_create_item(self, text, parent=None):
if parent:
#print('add to parent:', text, parent)
item = None
for i in range(parent.rowCount()):
child = parent.child(i)
#print('>> child:', child.text(), child)
if child.text() == text:
item = child
break
if item:
#print(' parent return:', item)
return item
else:
item = QtGui.QStandardItem(text)
parent.appendRow(item)
#print(' parent create:', text, item)
return item
else:
#print('add to model:', text)
items = self.model.findItems(text)
if items:
#print(' model return:', items[0])
return items[0]
else:
item = QtGui.QStandardItem(text)
self.model.appendRow(item)
#print(' model create:', text, item)
return item
完整的工作代码:
import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QTreeView, QVBoxLayout, QWidget
class TreeViewExample(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QTreeView Example")
self.setGeometry(100, 100, 800, 600)
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.tree_view = QTreeView(self.central_widget)
self.model = QtGui.QStandardItemModel()
self.tree_view.setModel(self.model)
# Replace this list with your own list of file paths
file_paths = [
"/home/user/documents/file1.txt",
"/home/user/documents/folder1/file2.txt",
"/home/user/documents/folder1/file3.txt",
"/home/user/pictures/photo1.jpg",
"/home/user/pictures/folder2/photo2.jpg",
]
# Organize file paths in parent-child hierarchy
self.organize_paths(file_paths)
# Set up the layout
layout = QVBoxLayout(self.central_widget)
layout.addWidget(self.tree_view)
def organize_paths(self, file_paths):
for path in file_paths:
components = path.split("/")[1:]
current_parent = None
for component in components:
#print('component:', component)
item = self.get_or_create_item(component, parent=current_parent)
current_parent = item
def get_or_create_item(self, text, parent=None):
if parent:
#print('add to parent:', text, parent)
item = None
for i in range(parent.rowCount()):
child = parent.child(i)
#print('>> child:', child.text(), child)
if child.text() == text:
item = child
break
if item:
#print(' parent return:', item)
return item
else:
item = QtGui.QStandardItem(text)
parent.appendRow(item)
#print(' parent create:', text, item)
return item
else:
#print('add to model:', text)
items = self.model.findItems(text)
if items:
#print(' model return:', items[0])
return items[0]
else:
item = QtGui.QStandardItem(text)
self.model.appendRow(item)
#print(' model create:', text, item)
return item
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TreeViewExample()
window.show()
sys.exit(app.exec())
结果: