PyQt5中树控件节点实现类qq好友变换分组拖拽实现
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class Demo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(400, 600)
self.tree=MyTreeWidget(self)
self.tree.setHeaderHidden(True)
self.tree.setDragEnabled(True)
self.tree.setAcceptDrops(True)
root=QTreeWidgetItem(self.tree)
root.setText(0, '新朋友')
child1=QTreeWidgetItem(root)
child1.setText(0, '好友1')
root1=QTreeWidgetItem(self.tree)
root1.setText(0, '家人')
child2=QTreeWidgetItem(root1)
child2.setText(0, '好友2')
class MyTreeWidget(QTreeWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setDefaultDropAction(Qt.MoveAction)
def dropEvent(self, event):
currentItem=self.itemAt(event.pos())
if currentItem != None and self.currentItem().parent() != None:
super().dropEvent(event)
if __name__=='__main__':
app=QApplication(sys.argv)
demo=Demo()
demo.show()
sys.exit(app.exec_())