Qt设置QTreeView的header为父节点名

时间:2024-11-18 17:45:20
  • from import *
  • from import *
  • from PyQt5.QtCore import *
  • class TreeModel(QAbstractItemModel):
  • def __init__(self, data, parent=None):
  • super().__init__(parent)
  • = TreeItem(("Name",))
  • self.setupModelData(data.split('\n'), )
  • def columnCount(self, parent):
  • if parent.isValid():
  • return parent.internalPointer().columnCount()
  • else:
  • return .columnCount()
  • def data(self, index, role=):
  • if not index.isValid():
  • return None
  • if role != and role != :
  • return None
  • item = index.internalPointer()
  • return item.data(index.column())
  • def flags(self, index):
  • if not index.isValid():
  • return
  • return super().flags(index) |
  • def headerData(self, section, orientation, role):
  • if orientation == and role == :
  • return .data(section)
  • return None
  • def index(self, row, column, parent):
  • if not self.hasIndex(row, column, parent):
  • return QModelIndex()
  • if not parent.isValid():
  • parentItem =
  • else:
  • parentItem = parent.internalPointer()
  • childItem = parentItem.child(row)
  • if childItem:
  • return self.createIndex(row, column, childItem)
  • else:
  • return QModelIndex()
  • def parent(self, index):
  • if not index.isValid():
  • return QModelIndex()
  • childItem = index.internalPointer()
  • parentItem = childItem.parent()
  • if parentItem == :
  • return QModelIndex()
  • return self.createIndex(parentItem.row(), 0, parentItem)
  • def rowCount(self, parent):
  • if parent.column() > 0:
  • return 0
  • if not parent.isValid():
  • parentItem =
  • else:
  • parentItem = parent.internalPointer()
  • return parentItem.childCount()
  • def setData(self, index, value, role=):
  • if role != :
  • return False
  • item = index.internalPointer()
  • result = item.setData(index.column(), value)
  • if result:
  • .emit(index, index, [, ])
  • return result
  • def setupModelData(self, lines, parent):
  • parents = [parent]
  • indentations = [0]
  • number = 0
  • while number < len(lines):
  • position = 0
  • while position < len(lines[number]):
  • if lines[number][position] != ' ':
  • break
  • position += 1
  • lineData = lines[number][position:].strip()
  • if lineData:
  • columnData = [i.strip() for i in lineData.split('\t')]
  • if position > indentations[-1]:
  • if parents[-1].childCount() > 0:
  • parents.append(parents[-1].child(parents[-1].childCount() - 1))
  • indentations.append(position)
  • else:
  • while position < indentations[-1] and len(parents) > 0:
  • parents.pop(-1)
  • indentations.pop(-1)
  • parent = parents[-1]
  • parent.insertChildren(parent.childCount(), 1, len(columnData))
  • for column in range(len(columnData)):
  • parent.child(parent.childCount() - 1).setData(column, columnData[column])
  • number += 1
  • class TreeItem:
  • def __init__(self, data=None, parent=None):
  • = parent
  • = data or []
  • = []
  • def appendChild(self, item):
  • .append(item)
  • def child(self, row):
  • return [row]
  • def childCount(self):
  • return len()
  • def columnCount(self):
  • return len()
  • def data(self, column):
  • try:
  • return [column]
  • except IndexError:
  • return None
  • def insertChildren(self, position, count, columns):
  • for row in range(count):
  • data = ["Parent {}".format(position), "Child {}".format(row)]
  • child = TreeItem(data, self)
  • .insert(position, child)
  • def parent(self):
  • return
  • def removeChild(self, position):
  • .pop(position)
  • def row(self):
  • if is not None:
  • return .index(self)
  • return 0
  • if __name__ == '__main__':
  • import sys
  • app = QApplication()
  • model = TreeModel("Name\tValue\n"
  • "Parent 1\tValue 1\n"
  • " Parent 2\tValue 2\n"
  • " Child 1\tValue 3\n"
  • " Child 2\tValue 4\n"
  • " Child 3\tValue 5\n"
  • " Parent 3\tValue 6")
  • treeView = QTreeView()
  • treeView.setModel(model)
  • treeView.setWindowTitle("Header as Parent Node")
  • treeView.resize(300, 400)
  • treeView.show()
  • sys.exit(app.exec_())