pyqt 树节点拖拽 QTreeWidget

目录

pyqt 树节点拖拽 改变层级顺序

pyqt 树节点拖拽 QTreeWidget 实例

主界面添加监听

自定义treewidget


pyqt 树节点拖拽 改变层级顺序

pyqt 树节点拖拽 QTreeWidget 实例

主界面添加监听

from PyQt5.QtWidgets import QApplication, QMainWindow, QTreeWidget, QTreeWidgetItem
from PyQt5.QtCore import Qt, QPoint

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setGeometry(300, 300, 600, 400)
        self.setWindowTitle('Tree Widget Drag and Drop Example')

        self.treeWidget = QTreeWidget(self)
        self.treeWidget.setAcceptDrops(True)
        self.treeWidget.setGeometry(10, 10, 580, 380)
        self.treeWidget.setHeaderLabels(['Items'])
        self.treeWidget.addTopLevelItem(QTreeWidgetItem(["Item 1"]))
        self.treeWidget.addTopLevelItem(QTreeWidgetItem(["Item 2"]))
        self.treeWidget.setDragEnabled(True)
        self.treeWidget.setDropIndicatorShown(True)
        self.treeWidget.setDragDropMode(QTreeWidget.InternalMove)

    def dragEnterEvent(self, event):
        event.accept()

    def dragMoveEvent(self, event):
        event.accept()

    def dropEvent(self, event):
        event.setDropAction(Qt.MoveAction)
        super().dropEvent(event)  # Ensure the internal move logic of QTreeWidget is used.

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

自定义treewidget

import sys
from PyQt5 import QtWidgets, QtCore

class CustomTreeWidget(QtWidgets.QTreeWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setHeaderLabels(['Items'])
        self.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
        self.setDragEnabled(True)
        self.setAcceptDrops(True)
        self.setDropIndicatorShown(True)

    def dropEvent(self, event):
        super().dropEvent(event)
        self.print_tree_structure()

    def print_tree_structure(self):
        def recurse(item, level=0):
            print(' ' * level + item.text(0))
            for i in range(item.childCount()):
                recurse(item.child(i), level + 1)

        root = self.invisibleRootItem()
        for i in range(root.childCount()):
            child = root.child(i)
            recurse(child)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('QTreeWidget Drag and Drop Example')
        self.setGeometry(300, 300, 400, 300)

        # 创建自定义的 QTreeWidget
        self.tree = CustomTreeWidget(self)
        self.setCentralWidget(self.tree)

        # 添加项目
        self.add_items()

    def add_items(self):
        root = QtWidgets.QTreeWidgetItem(self.tree, ['Root'])
        child1 = QtWidgets.QTreeWidgetItem(root, ['Child 1'])
        child2 = QtWidgets.QTreeWidgetItem(root, ['Child 2'])
        grandchild1 = QtWidgets.QTreeWidgetItem(child1, ['Grandchild 1'])

        # 展开所有节点
        self.tree.expandAll()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值