关联数据项选择的一个解决方案就是使用树视图。本文通过Python3+pyqt5实现了python Qt GUI 快速编程的16章的树视图例子。
/home/yrd/eric_workspace/chap16/treeoftable.py
#!/usr/bin/env python3
import bisect
import codecs
from PyQt5.QtCore import (QAbstractItemModel, QModelIndex,QVariant, Qt)
KEY, NODE = range(2)
class BranchNode(object):
def __init__(self, name, parent=None):
super(BranchNode, self).__init__()
self.name = name
self.parent = parent
self.children = []
def __lt__(self, other):
if isinstance(other, BranchNode):
return self.orderKey() < other.orderKey()
return False
def orderKey(self):
return self.name.lower()
def toString(self):
return self.name
def __len__(self):
return len(self.children)
def childAtRow(self, row):
assert 0 <= row < len(self.children)
return self.children[row][NODE]
def rowOfChild(self, child):
for i, item in enumerate(self.children):
if item[NODE] == child:
return i
return -1
def childWithKey(self, key):
if not self.children:
return None
# Causes a -3 deprecation warning. Solution will be to
# reimplement bisect_left and provide a key function.
i = bisect.bisect_left(self.children, (key, None))
if i < 0 or i >= len(self.children):
return None
if self.children[i][KEY] == key:
return self.children[i][NODE]
return None
def insertChild(self, child):
child.parent = self
bisect.insort(self.children, (child.orderKey(), child))
def hasLeaves(self):
if not self.children:
return False
return isinstance(self.children[0], LeafNode)
class LeafNode(object):
def __init__(self,
版权声明:本文为xiaoyangyang20原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。