从0开始搭建可视化二叉树
需要使用到的知识
数据结构
- 图
- 二叉树,二叉搜索树
- 堆
算法
- DFS
- BFS
数学
- 极角公式
- 勾股定理
库
Qt
框架
- QGraphicsView
- QGraphicsItem
- QGraphicsScene
第一步,继承框架
大坑
QGraphicsItemAnimation 和 QTimeLine
这是个大坑啊,animation和timeLine在创建新节点的时候永远是同一个地址,也就是引用,并且animation的setItem每次都需要重新设置。
自定义widget样式表失效
…
不理解的地方
-
QPropertyAnimation无法在没有继承QObject的地方使用,但是经过我的使用发现,我的类及时继承了QObject也无法使用,必须在原来就有继承QObject的类才能使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35class MyArrowLine(QGraphicsPathItem, QObject):
def __init__(self, startItem: QGraphicsItem, endItem: QGraphicsItem):
super().__init__()
super(QObject, self).__init__()
self.start = startItem.pos() # 起点
self.end = endItem.pos() # 终点
self.startI = startItem
self.endI = endItem
self._color = QColor(255, 255, 255)
# 设置Setting
self.setPen(QPen(Qt.blue, 2.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) # 颜色 | 大小
self.change() # 取最短路径
self.create_path() # 创建箭头
def line_color(self):
print("get_color")
return self._color
def line_color(self, color):
print("set_color")
self._color = color
self.setPen(QPen(Qt.black, 2.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
# 连接动画
def connection(self):
anim = QPropertyAnimation(self)
anim.setPropertyName(b'color')
anim.setTargetObject(self)
print("connection")
anim.setStartValue(Qt.white)
anim.setEndValue(Qt.blue)
anim.setDuration(2000)
anim.start()
未解决的BUG
- 在删除节点后的1秒内快速点击其他节点并拖动,此时将鼠标移动到View外面会发现widget会自动移动,目前不知道怎么解决该BUG。已经解决,只要在拖动item时将setAcceptDrops设置为False,释放鼠标时再设置为True就行了.
评论