class Tree(QtWidgets.QTreeWidget):
# ...
def dropEvent(self, event):
if event.source() == self:
event.setDropAction(QtCore.Qt.MoveAction)
super().dropEvent(event)
elif isinstance(event.source(), QtWidgets.QListWidget):
item = self.itemAt(event.pos())
ix = self.indexAt(event.pos())
col = 0 if item is None else ix.column()
item = self.invisibleRootItem() if item is None else item
ba = event.mimeData().data('application/x-qabstractitemmodeldatalist')
data_items = decode_data(ba)
for data_item in data_items:
it = QtWidgets.QTreeWidgetItem()
item.addChild(it)
for data in data_items:
for r, v in data.items():
it.setData(col, r, v)
class List(QtWidgets.QListWidget):
# ...
def dropEvent(self, event):
if event.source() == self:
event.setDropAction(QtCore.Qt.MoveAction)
QtWidgets.QListWidget.dropEvent(self, event)
elif isinstance(event.source(), QtWidgets.QTreeWidget):
item = self.itemAt(event.pos())
row = self.row(item) if item else self.count()
ba = event.mimeData().data('application/x-qabstractitemmodeldatalist')
data_items = decode_data(ba)
for i, data_item in enumerate(data_items):
it = QtWidgets.QListWidgetItem()
self.insertItem(row+i, it)
for r, v in data_item.items():
it.setData(r,v)
可拖动pyqt
最新推荐文章于 2024-03-28 20:21:39 发布
本文介绍如何在PyQT应用中实现界面组件的拖放操作,通过QDrag和QDropEvent等类,详细讲解了拖放事件的处理过程,帮助开发者创建更互动的图形用户界面。
摘要由CSDN通过智能技术生成