你需要实现一个dragEnterEvent、dragMoveEvent和dropEvent
要访问丢弃的 QTreeWidgetItem(s),您可以在 dropEvent 中执行此操作
class myTreeWidget(QtGui.QTreeWidget):
def __init__(self, parent = None):
super(myTreeWidget, self).__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
print('dragEnterEvent happened')
event.acceptProposedAction()
def dragMoveEvent(self, event):
print('dragMoveEvent happened')
event.acceptProposedAction()
def dropEvent(self, event):
print('dropEvent happened')
widgetItemThatMoved=event.source().currentItem()
parentThatReceivedIt=self.itemAt(event.pos())
self.theFunc(parentThatReceivedIt,widgetItemThatMoved )
event.acceptProposedAction()
def functionToCallWhenDropped(self,theFunc):
#this method allows you to pass a reference to this class so you assign it to a local variable for easy calling.
self.theFunc=theFunc
最好添加一个额外的方法 functionToCallWhenDropped 以便您可以访问父类方法,在父类中,您将执行类似这样的操作
def someMethod(self):
self.tw=myTreeWidget(parent)
self.tw.functionToCallWhenDropped(self.wasDropped)
def wasDropped(self, newParent, theMovedChild):
#this method will be called the very moment you drop the item
print(newParent.text(0)+" => "+theMovedChild.text(0))
我希望这可以帮助别人。