- Nodes of the same level can be dragged and prevented from entering each other, not other parent nodes.
I overwrote the dragMoveEvent
and dropEvent
methods in the QTreeWidget.I only have the second layer
effect correct, which can drag each other and prevent entry into each other. But the first and third levels
have different errors. For example, node1-1-1 and Node1-2-2 cannot be dragged to each other. Node1 can be dragged inside Node2, which is not in accordance with my finished requirements.
class TreeWidget(QTreeWidget):
def __init__(self):
super().__init__()
self.setDragDropMode(QTreeWidget.InternalMove)
def dragMoveEvent(self, event):
current_item = self.currentItem()
item = self.itemAt(event.pos())
if current_item and current_item.type() == 0:
super().dragMoveEvent(event)
elif item and item.type() == 1 and current_item.parent() == item.parent():
super().dragMoveEvent(event)
elif item and item.type() == 2 and current_item.parent() == item.parent():
super().dragMoveEvent(event)
else:
event.ignore()
def dropEvent(self, event):
current_item = self.currentItem()
item = self.itemAt(event.pos())
if current_item and current_item.type() == 0:
super(TreeWidget, self).dropEvent(event)
elif item and item.type() == 1 and current_item.parent() == item.parent():
super(TreeWidget, self).dropEvent(event)
elif item and item.type() == 2 and current_item.parent() == item.parent():
super(TreeWidget, self).dropEvent(event)
else:
event.ignore()
class BasicTreeTest1(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('QTreeWidget')
self.tree = TreeWidget()
root1 = QTreeWidgetItem(self.tree,type=0)
root1.setText(0, 'node1')
child1_1 = QTreeWidgetItem(root1,type=1)
child1_1.setText(0, 'node1-1')
child1_1.setFlags(child1_1.flags() & ~Qt.ItemIsDropEnabled)
child1_2 = QTreeWidgetItem(root1, type=1)
child1_2.setText(0, 'node1-2')
child1_2.setFlags(child1_2.flags() & ~Qt.ItemIsDropEnabled)
child1_1_1 = QTreeWidgetItem(child1_1, type=2)
child1_1_1.setText(0, 'node1-1-1')
child1_1_1.setFlags(child1_1_1.flags() & ~Qt.ItemIsDropEnabled)
child1_2_1 = QTreeWidgetItem(child1_1, type=2)
child1_2_1.setText(0, 'node1-2-1')
child1_2_1.setFlags(child1_2_1.flags() & ~Qt.ItemIsDropEnabled)
root2 = QTreeWidgetItem(self.tree,type=0)
root2.setText(0, 'node2')
child2_1 = QTreeWidgetItem(root2, type=1)
child2_1.setText(0, 'node2-1')
child2_1.setFlags(child2_1.flags() & ~Qt.ItemIsDropEnabled)
child2_2 = QTreeWidgetItem(root2, type=1)
child2_2.setText(0, 'node2-2')
child2_2.setFlags(child2_2.flags() & ~Qt.ItemIsDropEnabled)
root3 = QTreeWidgetItem(self.tree, type=0)
root3.setText(0, 'node3')
child3_1 = QTreeWidgetItem(root3, type=1)
child3_1.setText(0, 'node3_1')
child3_1.setFlags(child3_1.flags() & ~Qt.ItemIsDropEnabled)
child3_2 = QTreeWidgetItem(root3, type=1)
child3_2.setText(0, 'node3_2')
child3_2.setFlags(child3_2.flags() & ~Qt.ItemIsDropEnabled)
child3_2_1 = QTreeWidgetItem(child3_2, type=2)
child3_2_1.setText(0, 'node3-2-1')
child3_2_1.setFlags(child3_2_1.flags() & ~Qt.ItemIsDropEnabled)
child3_2_2 = QTreeWidgetItem(child3_2, type=2)
child3_2_2.setText(0, 'node3-2-2')
child3_2_2.setFlags(child3_2_2.flags() & ~Qt.ItemIsDropEnabled)
# root1.setFlags(root1.flags() & ~Qt.ItemIsDropEnabled)
# root2.setFlags(root2.flags() & ~Qt.ItemIsDropEnabled)
# root3.setFlags(root3.flags() & ~Qt.ItemIsDropEnabled)
# child22.setFlags(child22.flags() & ~Qt.ItemIsDropEnabled)
self.setCentralWidget(self.tree)
self.tree.expandAll()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = BasicTreeTest1()
w.show()
sys.exit(app.exec_())
This site is my reference: https://blog.csdn.net/this_is_id/article/details/125258736