Results 1 to 3 of 3

Thread: How to Sort a QModelIndexList

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to Sort a QModelIndexList

    I have a QModelIndexList from "selectedRows()" from a table.

    How can I sort this list in reverse order so I can use this list to remove rows while keeping the indices valid.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    8
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: How to Sort a QModelIndexList

    Although it is pretty late to this thread, I am posting my solution in case I forget in future.

    Qt Code:
    1. QModelIndexList indexlist = this->lastView->selectionModel()->selectedRows();
    2. qSort(indexlist.begin(), indexlist.end(), qGreater<QModelIndex>()); // so that rows are removed from highest index
    3. foreach (QModelIndex index, indexlist) {
    4. index.model()->removeRow(index.row());
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to azalea for this useful post:

    Dariusz (5th November 2017), d_stranz (31st August 2016)

  4. #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: How to Sort a QModelIndexList

    I was going to propose simply using the QList reverse iterator to go from the end of the list backwards, but that assumes the list is sorted in increasing QModelIndex order in the first place. azalea's code doesn't make that assumption, so it is a better and more general solution.
    <=== 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.

Similar Threads

  1. How to sort a Qlist AND get a sort key?
    By agerlach in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 18:44
  2. QModelIndexList access reading violation
    By Daxos in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2010, 08:32
  3. QModelIndexList crashes application on destruct
    By mstegehu in forum Qt Programming
    Replies: 10
    Last Post: 19th May 2010, 08:58
  4. multiple sort
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 25th August 2009, 08:43
  5. Hot to get a QModelIndexList from a model?
    By brazso in forum Newbie
    Replies: 1
    Last Post: 16th January 2009, 12:10

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.