1

I want to copy an item from a QTreeWidget-parent under another parent via a drag & drop mouse operation. For this, I have implemented the dropEvent() and am setting the dropAction to Qt.CopyAction. But anyway, the item I am dropping is not being copied under the new parent. E.g. -> dragging the user "schmidt" under the group "LON".

Expected behaviour: the item I am dropping is being copied under the new parent. (e.g. user "schmidt" will be added under group "LON").

Full working code example:

#!/usr/bin/env python3
# coding = utf-8
import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class MyTreeWidget(QtWidgets.QTreeWidget):
    def __init__(self):
        QtWidgets.QTreeView.__init__(self)        
        self.setSelectionMode(self.SingleSelection)
        self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
        self.setDragEnabled(True)
        self.setAcceptDrops(True)
        self.setDropIndicatorShown(True)

    def dropEvent(self, event):        
        event.setDropAction(QtCore.Qt.CopyAction)
        event.accept()       

class MyMainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.tree = MyTreeWidget()
        self.tree.setRootIsDecorated(True)
        self.tree.setHeaderHidden(True)

        self.setCentralWidget(self.tree)

        itemUsers = QtWidgets.QTreeWidgetItem(self.tree, ["User"])
        itemUsers.addChild(QtWidgets.QTreeWidgetItem(itemUsers, ["schmidt"]))
        itemUsers.addChild(QtWidgets.QTreeWidgetItem(itemUsers, ["weber"]))

        itemMdt = QtWidgets.QTreeWidgetItem(self.tree, ["Group"])
        itemMdt.addChild(QtWidgets.QTreeWidgetItem(itemMdt, ["FFM"]))
        itemMdt.addChild(QtWidgets.QTreeWidgetItem(itemMdt, ["LON"]))
        itemMdt.addChild(QtWidgets.QTreeWidgetItem(itemMdt, ["NY"]))

        self.show()
        self.setGeometry(400, 400, 400, 400)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ui = MyMainWindow()    
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ProfP30
  • 358
  • 3
  • 18

1 Answers1

0

Remove the dropEvent method.

TL; DR;

It is not necessary to override the dropEvent method since it has already implemented all the logic to copy the items.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks. And how'd I've to implement a validation that the item already exists under the new parent and then skip it if that's the case? – ProfP30 Nov 18 '19 at 17:13
  • @ProfP30 You can override the dropEvent method and call super() if it is valid: `def dropEvent(self, event): if validation_function(event): super().dropEvent(event)` – eyllanesc Nov 18 '19 at 17:15
  • Thanks! How could I evalaute the dropped data (e.g. text of the dropped QTreeWidgetItem. The source-parameter contains only the QTreeWidgets name. And how could I iterate over all the new-parents existing children to compare with the former. I would like to check if the dropped item already exists as child of the new parent. – ProfP30 Nov 18 '19 at 17:38
  • @ProfP30 Then get the mimedata information of the event, for example create a fake_model (as an implement in this solution: https://stackoverflow.com/a/55740109/6622587) and get the item. – eyllanesc Nov 18 '19 at 17:42
  • Thank you. How could I set the icon of this newly inserted item after it got inserted via `super().dropEvent(event)`? – ProfP30 Dec 27 '19 at 12:47
  • -> filed a new question under following link: https://stackoverflow.com/questions/59530061/qtreeview-setting-icon-for-item-on-dropevent – ProfP30 Dec 30 '19 at 11:28