Hi there,
I'm having a little bit of trouble with the ordering of tree nodes selected in a QTreeView. I want to create a textual representation of tree nodes selected in a QTreeView and store it on the clipboard. Here is an example of how such a tree could look like:
Path 1902
path_group
startpoint
endpoint
delay_type
[...]
The indented nodes are child nodes of the node named "Path 1902". The selection handling in this QTreeView is configured as follows:
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::ExtendedSelection);
To copy to clipboard, switch view to plain text mode
When I select the tree elements shown above starting with "Path 1902" and until (and including) "delay_type" then afterwards I can iterate over the selected elements as follows:
for (QModelIndex index
: selItemsModel
->selectedIndexes
()) {
// Create textual representation of selected tree nodes.
}
QItemSelectionModel *selItemsModel(selectionModel());
for (QModelIndex index : selItemsModel->selectedIndexes()) {
// Create textual representation of selected tree nodes.
}
To copy to clipboard, switch view to plain text mode
My problem with this procedure is that the ordering of the selected items is different from what I'd expect. It looks as follows:
path_group
startpoint
endpoint
delay_type
Path 1902
As can be seen the selected tree nodes seem to be ordered from bottom to top, i.e. first the child nodes and then their parent.
I'd prefer to have the selected items ordered from top to bottom. I studied the class documentation of QItemSelectionModel. In the code snippet above I used the method selectedIndexes() to get the indexes of the selected tree items. There is another method selectedRows() in class QItemSelectionModel but it seems to return the selected nodes in the same order.
I wonder if there is a possibility to retrieve the selected nodes ordered from top to bottom.
Any ideas?
Bookmarks