我是一位翻译助手,为您翻译以下与IT技术相关的内容。您需要在QTreeView中表示一个列表,但是找了很久都没有找到令人满意的答案。
以下是示例:
['.', ['A', 'B', 'C', 'D', 'E', 'F']], ['A', ['G', 'H']], ['A\\G', []], ['A\\H', ['K', 'L']], ['A\\G\\K', []], ['A\\G\\L', []], ['B', ['I', 'J']], ['B\\I', []], ['B\\J', []], ['C', []], ['D', []], ['E', []], ['F', ['M']], ['F\\M', []]
应该这样表示:
.
|____A
|____H
|____G
|____K
|____L
|____B
|____I
|____J
|____C
|____D
|____E
|____F
|____M
编辑:也许数据准备得不太好。我也可以采用这种结构:
[".", "A", "A\\G", "A\\H", "A\\H\\K", "A\\H\\L", "B", "B\\I", "B\\J", "C", "D", "E", "F", "F\\M"]
在控制台中可视化该结构不是问题,但我无法正确地在树形视图中显示它。这是我的节点类和树模型。
from PySide.QtCore import *
from PySide.QtGui import *
import sys
class Node(object):
def __init__(self, name, parent=None):
self._name = name
self._children = []
self._parent = parent
if parent is not None:
parent.add_child(self)
def type_info(self):
return 'NODE'
def add_child(self, child):
self._children.append(child)
def insert_child(self, position, child):
if position < 0 or position > len(self._children):
return False
self._children.insert(position, child)
child._parent = self
return True
def remove_child(self, position):
if position < 0 or position > len(self._children):
return False
child = self._children.pop(position)
child._parent = None
return True
def name(self):
return self._name
def set_name(self, name):
self._name = name
def child(self, row):
return self._children[row]
def child_count(self):
return len(self._children)
def parent(self):
return self._parent
def row(self):
if self._parent is not None:
return self._parent._children.index(self)
def log(self, tab_level=-1):
output = ''
tab_level += 1
for i in range(tab_level):
output += '\t'
output += '|____' + self._name + '\n'
for child in self._children:
output += child.log(tab_level)
tab_level -= 1
return output
def __repr__(self):
return self.log()
class TemplateTreeModel(QAbstractItemModel):
def __init__(self, root, parent=None):
super(TemplateTreeModel, self).__init__(parent)
self._root_node = root
def rowCount(self, parent):
if not parent.isValid():
parent_node = self._root_node
else:
parent_node = parent.internalPointer()
return parent_node.child_count()
def columnCount(self, parent):
return 1
def data(self, index, role):
if not index.isValid():
print('root')
return None
node = index.internalPointer()
if role == Qt.DisplayRole or role == Qt.EditRole:
if index.column() == 0:
return node.name()
if role == Qt.DecorationRole:
if index.column() == 0:
type_info = node.type_info()
if type_info == 'FOLDER':
return QIcon('src/dir.png')
def setData(self, index, value, role=Qt.EditRole):
if index.isValid():
if role == Qt.EditRole:
node = index.internalPointer()
node.set_name(value)
return True
return False
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole:
if section == 0:
return 'Templates'
else:
return 'Type'
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
def parent(self, index):
node = self.get_node(index)
parent_node = node.parent()
if parent_node == self._root_node:
return QModelIndex()
return self.createIndex(parent_node.row(), 0, parent_node)
def index(self, row, column, parent):
parent_node = self.get_node(parent)
child_item = parent_node.child(row)
if child_item:
return self.createIndex(row, column, child_item)
else:
return QModelIndex
def get_node(self, index):
if index.isValid():
node = index.internalPointer()
if node:
return node
return self._root_node
def insertRows(self, position, rows, parent=QModelIndex()):
parent_node = self.get_node(parent)
self.beginInsertRows(parent, position, position + rows - 1)
for row in range(rows):
child_count = parent_node.child_count()
child_node = Node('untitled' + str(child_count))
success = parent_node.insert_child(position, child_node)
self.endInsertRows()
return success
def removeRows(self, position, rows, parent=QModelIndex()):
parent_node = self.get_node(parent)
self.beginRemoveRows(parent, position, position + rows - 1)
for row in range(rows):
success = parent_node.remove_child(position)
self.endRemoveRows()
return success
有什么好的方法可以正确地做到这一点吗?
for root, dirs in structure_list:
depth = root.split(os.sep)
print((len(depth) - 1) * '---', os.path.basename(root))
这是我对控制台可视化的大致做法。谢谢!