重载拖曳的几个函数,需要注意mime类型,QTreeView drag的数据类型是application/x-standarditemmodeldatalist,Pyqt默认提供的几个高级helper函数不够用。
class MyPlotWidget(pg.PlotWidget):
'''
拖曳事件
'''
def init(self, parent=None):
super().init(parent)
self.setAcceptDrops(True)
def dragMoveEvent(self, event):
src=event.source()
if src and src!=self:
event.setDropAction(Qt.MoveAction)
def dragEnterEvent(self, event):
#改指示
if event.mimeData().hasFormat('application/x-qstandarditemmodeldatalist'):
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
data = event.mimeData()
source_item = QStandardItemModel()
source_item.dropMimeData(data, Qt.CopyAction,0,0,QModelIndex())
print(source_item.item(0, 0).text())