当在QTreeWidget中拖放QTreeWidgetItems时,如何捕获被移动的项以便检索其新父元素?

时间:2021-01-26 15:05:45

I have a simple TreeWidget populated with subclassed TreeWidgetItems. When one of these Items is moved through an Internal Drag and Drop event, how do I catch the Item that was moved?

我有一个由子类TreeWidgetItems填充的简单TreeWidget。当其中一个项通过内部拖放事件移动时,如何捕获移动的项?

When the Item is moved, I want to catch the item so i can get its new parent item.

当该项被移动时,我希望捕获该项,以便获得它的新父项。

I have tried using an eventFilter to look for a ChildRemoved event, but when I ask the event for the child, it returns a generic QObject, instead of my subclassed item. What am I missing?

我尝试过使用eventFilter来查找一个ChildRemoved事件,但是当我为子事件请求事件时,它会返回一个通用的QObject,而不是我的子类项。我缺少什么?

def eventFilter(self, sender, event):
    if event.type() == QtCore.QEvent.ChildRemoved:
        widgetItemThatMoved = event.child()
        self.updateSomething(widgetItemThatMoved)
    return False

Thanks!

谢谢!

1 个解决方案

#1


1  

you will need to implement a dragEnterEvent, dragMoveEvent and dropEvent

您将需要实现一个dragEnterEvent、dragMoveEvent和dropEvent。

To access the dropped QTreeWidgetItem(s), you do that right inside the 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

It will be nice to add an extra method functionToCallWhenDropped so that you can have access to the parent class method, in the parent class, you will do something like this

添加一个额外的方法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))

I hope this helps someone.

我希望这能帮助某人。

#1


1  

you will need to implement a dragEnterEvent, dragMoveEvent and dropEvent

您将需要实现一个dragEnterEvent、dragMoveEvent和dropEvent。

To access the dropped QTreeWidgetItem(s), you do that right inside the 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

It will be nice to add an extra method functionToCallWhenDropped so that you can have access to the parent class method, in the parent class, you will do something like this

添加一个额外的方法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))

I hope this helps someone.

我希望这能帮助某人。