Using QTreeWidget drag & drop with QDomDocument
In my application, I have a QTreeWidget that allows the user to reorganize the ordering of its items by dragging and dropping them. I also have a QDomDocument that represents the structure of the QTreeWidget, so when the user moves an item, I want the position of the nodes in the QDomDocument to be updated to reflect this new arrangement. The nodes are uniquely identified by their attributes.
What functions do I need to use or reimplement to accomplish this? I found the QDomNode::insertBefore() and insertAfter() functions, so it seems like I just need to remove the node from its original position and then call insertBefore() or insertAfter() to put it in its new position. I think I need to reimplement QTreeWidget::dropEvent, and then do the QDom updating in there. But how can I detect the original position and the new position of the node inside dropEvent?
Thanks for any help you can give.
first you need to make sure that the drag contains the data you need to identify the domnode. This can be some custom mime type. This can be done by implementing a custom model and reimplement "QAbstractItemModel::dropMimeData()":http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#dropMimeData
Then you can reimplement "QTreeWidget::dropMimeData()":http://qt-project.org/doc/qt-4.8/qtreewidget.html#dropMimeData and read your custom mime data there.
I'm sorry, I don't quite understand what you are suggesting. Do you propose replacing my QTreeWidget with a reimplementation of QAbstractItemModel in order to have a copy of the item's title inside dropMimeData()? I was planning on simply using QTreeWidget::selectedItems() inside dropEvent() and thereby obtaining the title of the dragged item, which I can use to find the corresponding node in the QDomDocument. My QTreeWidget only allows selection of one item at a time so there is no ambiguity.
Also, I still don't know how I can detect the new position of the QTreeWidgetItem once it is dropped, which is the real heart of my question.