Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Multi-level Tree indexes for Drag and Drop?
spazvt last edited by
I'm using a subclassed view of QTreeView to display a 2-level deep tree structure that is built on QAbstractItemModel. In the same application window, I'm also showing a graph of real-time data, which is generated by emitted signals when the model's data is updated. What I want to do is drag a row/item from the TreeView into the graph area, and when the mouse is released and I get a dropEvent() call in the Graph object, I want to know which item in the model was Dragged-and-Dropped so that I can begin displaying its realtime data. My issue, though, is that I don't know what 'modelIndex' parent is being used, whether I got a level1 item (and thus the root is the parent) or a level2 item (and thus a level1 item is the parent).
Right now, the default mimeData() I receive from the DnD operation is packaged by QAbstractItemView::startDrag() 's call to QAbstractItemModel::mimeData(), which packs it in the "application/x-qabstractitemmodeldatalist" format, which is basically an index (row, column) followed by the data. From my debugging, the index received does correspond to the row I see in the GUI, but if I 'expand' an item to see the next level of items below it, and then DnD one of them into the graph, I only get the index of the sub item within the expanded item. But in my code I won't know whether the index received is in reIation to the top-level model's root, or in relation to a lower-level branch. It seems to me that QItemSelectionModel only keeps track of a list of selected indexes within a single layer of the model, rather than providing an index for each descending branch in the tree. In the current architecture I was hoping for but couldn't find something that provided a series of hierarchical indexes between the root of the model and the selected item.
So in summary, does anyone know a method of figuring out the location of the currently selected item within a multiple-level tree model? I'll then encode this data in my mimeData() function to be passed to my Graph in the DnD operation. I'm guessing QT provides a method and I just can't find it.
DerManu last edited by
Just to clarify:
With model->selectionModel()->selectedIndexes() you get a QList<QModelIndex> of selected indexes. On the QModelIndex, you can call parent() to get the parent index and thus have a possibility to find the complete path up to the root index (in which case parent().isValid() returns false). isn't this what you need?