Results 1 to 4 of 4

Thread: Issue with Ordering of selected Nodes in a QTreeView

  1. #1
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Issue with Ordering of selected Nodes in a QTreeView

    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:

    Qt Code:
    1. setSelectionBehavior(QAbstractItemView::SelectRows);
    2. 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:

    Qt Code:
    1. QItemSelectionModel *selItemsModel(selectionModel());
    2. for (QModelIndex index : selItemsModel->selectedIndexes()) {
    3.  
    4. // Create textual representation of selected tree nodes.
    5.  
    6. }
    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?

  2. #2
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue with Ordering of selected Nodes in a QTreeView

    The documentation clearly states that the selection is not sorted:

    Returns a list of all selected model item indexes. The list contains no duplicates, and is not sorted.

    Means you have to sort the QModelIndexList yourself:

    Qt Code:
    1. QModelIndexList selection = selItemsModel->selectedIndexes();
    2. sort(selection.begin(), selection.end(),
    3. [](const QModelIndex & a, const QModelIndex & b)
    4. {
    5. return a.row() < b.row();
    6. });
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issue with Ordering of selected Nodes in a QTreeView

    And since you are selecting on multiple levels of the tree hierarchy, your sort condition needs to not only take into account the row, but also the parent-child relationship of those rows. (E.g. both "Path 1902" and "path_group" could have a row index of zero).

    This would be especially important if you selected two top-level groups - then you would have duplicate row indexes for each of the two "Path" parents.

    If what you want to do is to allow extended selection, but in effect select all children of the selected "Path" nodes, then I would iterate over the returned list of QModelIndex entries, pull out only the Path nodes into another list and sort that new list by row number. Then I would go over that sorted list and pull all of the child nodes of each Path node out of the model and insert those into the sorted list following their parent Path node. Everything will then be in the same order as it appears in the tree.

    In effect, you ignore the child nodes returned by the initial selection, but then go back into the model and pull out the complete list of them based on the Path nodes that were selected.
    Last edited by d_stranz; 14th September 2018 at 18:48.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Thumbs up Re: Issue with Ordering of selected Nodes in a QTreeView

    Thanks a lot, d_stranz for your valuable hints! You identified my problem absolutely correct. And your proposal how to manage the proper sorting of the selected tree nodes convinced me completely. So I'll go for it.

Similar Threads

  1. Simple way to expand all nodes on a QTreeView?
    By cboles in forum Qt Programming
    Replies: 10
    Last Post: 12th April 2014, 17:54
  2. QTreeView select parent nodes
    By mqt in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2013, 06:01
  3. Replies: 0
    Last Post: 22nd October 2011, 13:52
  4. Replies: 1
    Last Post: 28th February 2007, 09:34
  5. [QT4] QTreeView and expandable nodes
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2006, 17:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.