可拖动pyqt
作者:互联网
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)
标签:拖动,items,self,pyqt,QtWidgets,item,data,event 来源: https://blog.csdn.net/ailinyingai/article/details/116771477