Drag/drop item without replacing it with a fresh instance
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): super(QTreeViewCustom,self).__init__() self.setDragDropMode(QtGui.QAbstractItemView.InternalMove) self.setDragEnabled(True) self.setDropIndicatorShown(True) self.dataModel = QtGui.QStandardItemModel() self.root = self.dataModel.invisibleRootItem() self.root.setDropEnabled(True) self.setModel(self.dataModel) self.setSelectionMode(QtGui.QAbstractItemView.SelectionMode.ContiguousSelection) #fill the tree with items for i in range(10): self.root.appendRow(testItem(str(i))) class testItem(QtGui.QStandardItem): def __init__(self,name): super(testItem,self).__init__() self.customAttr = 'test' self.setText(name)
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.
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'?
Take a look at the Drag and Drop with item views in Qt's documentation.