Important: Please read the Qt Code of Conduct -

Drag/drop item without replacing it with a fresh instance

  • Hi all,

    I have a QTreeView, filled with a bunch custom version of the QStandardItem class (gave it some extra attributes i need).

    I need to be able to reorder the items in the QTreeView by dragging them around. This already works perfectly. However, whenever I make a modification to an item, that item will be replaced by a fresh instance of a QStandardItem, therefore losing all the custom stuff I added to my custom version of the QStandardItem.

    Is there a way to be able to reorder stuff by dragging, but instead of replacing it with a new item, just keeping the original? Below is a very basic version of my code.



    from PySide import QtGui, QtCore
    class QTreeViewCustom(QtGui.QTreeView):
        def __init__(self):
            self.dataModel = QtGui.QStandardItemModel()
            self.root = self.dataModel.invisibleRootItem()
            #fill the tree with items
            for i in range(10):
    class testItem(QtGui.QStandardItem):
        def __init__(self,name):
            self.customAttr = 'test'

  • Lifetime Qt Champion


    Isn't that the same question you posted here ?

  • @SGaist Correct, sorry about that.

    As this is such an active forum, that thread got buried pretty quickly, without getting an answer. Therefore I figured modifying my opening post and title a little bit and trying it again. Hoping someone who can give me a psuh in the right direction would see it.

  • Lifetime Qt Champion

    Doing so you are essentially fragmenting the information. If the original thread doesn't suit then you should rather delete it to start fresh but you'll also lose the original content.

    In any case, since you are using custom items, you should also handle the drop yourself.

  • Could you elaborate further on what you mean by 'you should also handle the drop yourself'?

  • Lifetime Qt Champion

    Take a look at the Drag and Drop with item views in Qt's documentation.

Log in to reply