python+pyqt5实现多层级复选框选中
- 效果
- 如何插入一段漂亮的代码片
效果
如何插入一段漂亮的代码片
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
class filterWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
self.setLayout(layout)
self.tree = QTreeWidget()
self.tree.setHeaderHidden(True) # 隐藏表头
self.tree.resize(300,600)
self.tree.setColumnCount(2)
self.tree.setColumnWidth(0,300)
# root Node
root = QTreeWidgetItem(self.tree)
root.setText(0, "Root")
root.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsSelectable )
root.setCheckState(0, Qt.Checked)
# add child to root
child1 = QTreeWidgetItem(root)
child1.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsSelectable )
child1.setText(0, "Child 1")
child1.setCheckState(0, Qt.Checked)
# 添加子节点2
child2 = QTreeWidgetItem(root)
child2.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsSelectable )
child2.setText(0, '子节点2')
child2.setCheckState(0, Qt.Checked)
# 为child2添加一个子节点
child3 = QTreeWidgetItem(child2)
child3.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsSelectable )
child3.setText(0, '子节点3')
child3.setCheckState(0, Qt.Checked)
# 为child2添加一个子节点
child4 = QTreeWidgetItem(child2)
child4.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
child4.setText(0, '子节点4')
child4.setCheckState(0, Qt.Checked)
# 默认所有节点都处于展开状态
self.tree.expandAll()
self.tree.itemChanged.connect(self.onTreeItemChanged)
layout.addWidget(self.tree)
def onTreeItemChanged(self, item, column):
def get_Node_parent(item):
if item.parent():
return item.parent()
def get_Node_child(item,childList):
if item.childCount() == 0:
childList.append(item)
if item.childCount() > 0:
for i in range(item.childCount()):
child_item = item.child(i)
childList.append(child_item)
get_Node_child(child_item,childList)
return childList
def get_sample_level(item,is_sample = None):
level = []
sample_node = item.parent()
if sample_node.childCount() == 1:
level.append(item)
elif sample_node.childCount() > 1:
for i in range(sample_node.childCount()):
level.append(sample_node.child(i))
if is_sample is None or is_sample is False:
return level
# 判断所有节点状态是否一致
first_state = level[0].checkState(0)
all_same_state = all(item.checkState(0) == first_state for item in level)
if all_same_state:
return True
else:
return False
def get_All_Node(item):
items = []
for i in range(item.childCount()):
child = item.child(i)
items.append(child)
items.extend(get_All_Node(child))
return items
# 选中叶子节点
if item.childCount() == 0:
Node_parent = get_Node_parent(item)
sample_level_code = get_sample_level(item, is_sample=True)
if item.checkState(0) != Node_parent.checkState(0):
# 检查其他节点的状态是否与第一个节点一致
if sample_level_code:
Node_parent.setCheckState(0, item.checkState(0))
else:
Node_parent.setCheckState(0, Qt.PartiallyChecked)
# 选中根节点
elif item.parent() is None:
if item.checkState(0) == Qt.PartiallyChecked:
return
Node_All = get_All_Node(item)
if item.checkState(0) == Qt.Checked:
for i in Node_All:
if i.checkState(0) != Qt.Checked:
i.setCheckState(0, Qt.Checked)
elif item.checkState(0) == Qt.Unchecked:
for i in Node_All:
if i.checkState(0) != Qt.Unchecked:
i.setCheckState(0, Qt.Unchecked)
elif item.parent() is not None and item.childCount() > 0:
Node_parent = get_Node_parent(item)
sample_level_code = get_sample_level(item,is_sample = True)
# 更新父节点的状态
if sample_level_code:
Node_parent.setCheckState(0, item.checkState(0))
else:
Node_parent.setCheckState(0, Qt.PartiallyChecked)
if item.checkState(0) != Qt.PartiallyChecked:
# 更新子节点的状态
child_list = get_Node_child(item,[])
for i in child_list:
i.setCheckState(0, item.checkState(0))
def add_child_with_checkbox(self, parent, depth):
if depth == 0:
return
for i in range(3):
child = QTreeWidgetItem()
child.setFlags(child.flags() | 0x20) # 添加 ItemIsUserCheckable 标志
child.setText(0, f"Child {i + 1}")
parent.addChild(child)
self.add_child_with_checkbox(child, depth - 1)