Results 1 to 15 of 15

Thread: How can I remove a list of selected items in the QListView in QT 4.6.?

  1. #1
    Join Date
    Jul 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How can I remove a list of selected items in the QListView in QT 4.6.?

    Hi. How can I remove a list of selected items in the QListView in QT 4.6.?
    Something like this does not work, the iterator becomes invalid:

    Qt Code:
    1. QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
    2. foreach(QModelIndex index, indexes)
    3. {
    4. model->removeRow(index.row());
    5. }
    To copy to clipboard, switch view to plain text mode 

    removeRows also not suitable, it removes N-items that follows the one given.
    I use QStandardItemModel to store items.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Something like that should work. (Idea: sort the list and begin at the end to ensure that the indices stay valid):
    Qt Code:
    1. YOURVIEW->setUpdatesEnabled(false);
    2. QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
    3. indexes.sort();
    4. for (i = indexes.count() - 1; i > -1; --i)
    5. model->removeRow(indexes.at(i).row());
    6. YOURVIEW->setUpdatesEnabled(true);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    saa7_go (26th July 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Here's the solution:
    Qt Code:
    1. QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
    2. while(indexes.size()) {
    3. model->removeRow(indexes.first().row());
    4. indexes = ui.listview_files->selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Leviathan for this useful post:

    saa7_go (25th July 2010)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    There is useless overhead here. Lykurg's solution is much better. And provided the list is already sorted, you can simply start removing items from the end.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    saa7_go (26th July 2010)

  8. #5
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Okay, i tried Lykurg's code. But, there is build issues informed by Qt Creator.
    O:/Qt/DeleteSelectedItemInListView/form.cpp:53: error: 'class QModelIndexList' has no member named 'sort'
    Then, i looked at Qt Assistant, especially in QList Class Reference. There is no sort() function.
    But, i found qSort() function when searching for keyword sort.

    Then, I modified Lykurg's code like following.
    Qt Code:
    1. ui->listView->setUpdatesEnabled(false);
    2. QModelIndexList indexes = ui->listView->selectionModel()->selectedIndexes();
    3. qSort(indexes.begin(), indexes.end());
    4.  
    5. for(int i = indexes.count() - 1; i > -1; --i)
    6. model->removeRow(indexes.at(i).row());
    7.  
    8. ui->listView->setUpdatesEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    And that is work. Thanks for your sugesstion, wysota.

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Check if you have to sort the list at all. As far as I remember it is already sorted and sorting a sorted list is an expensive operation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Since I also wasn't sure, I've looked up and found:
    QModelIndexList QItemSelectionModel::selectedIndexes () const

    Returns a list of all selected model item indexes. The list contains no duplicates, and is not sorted.
    So you have to sort the list.

  11. #8
    Join Date
    Sep 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Hello Guys,

    I just had the same question, thanks for resolving!
    But there is something more I'm interested in:

    I want to connect the "removing"-thing with a button. How does this work?

  12. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    What is the "removing-thing"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #10
    Join Date
    Sep 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Qt Code:
    1. ui->listView->setUpdatesEnabled(false);
    2. QModelIndexList indexes = ui->listView->selectionModel()->selectedIndexes();
    3. qSort(indexes.begin(), indexes.end());
    4.  
    5. for(int i = indexes.count() - 1; i > -1; --i)
    6. model->removeRow(indexes.at(i).row());
    7.  
    8. ui->listView->setUpdatesEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    I ment the way to remove a list of selected items in the QListView.
    Now I want to connect this with a button (called "Delete"). If you select the items and press delete-button, they shall be removed with that.

    Thanks for beeing interested in my problem! I would be happy to read an idea, not the whole solving please.

  14. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    So what is the problem? Make a signal-slot connection and put your code in a custom slot of yours.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #12
    Join Date
    Sep 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    No problem, I actually did this before.
    But I got my items from a rootpath. So I dont geht the updates "deleted-list", no he always shows me the pathlist.
    Last edited by Bloody; 14th September 2012 at 11:09.

  16. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Quote Originally Posted by Bloody View Post
    But I got my items from a rootpath. So I dont geht the updates "deleted-list", no he always shows me the pathlist.
    I don't understand what you mean by that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #14
    Join Date
    Sep 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    Sorry, that I could not answer these days. So actually I want help..

    To my problem: I want a listfiew displaying files from a rootpath. 'Till here the top solution is pretty helpful. But the next step I want to go is to delete some selected files but the solution in this thread is not working as I want to.
    I select items i want to delete(working well), press the "delete files" button and nothing happens. I guess the problem here is the self-updating listview. Over and above I actually want to build a "copy-program". Therefor you select some files from the listview and press "delete". Then you "save" the rest in a new folder. The system creates a new folder in any path with a copy of the left files in the listfiew.
    But back to my update problem: If I press delete, nothing happens... ? And if I create new files in my path, they self-update into the listview. My idea: How to create listviewitems which are actually files able to be deleted out of the listview? How about this idea?

  18. #15
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can I remove a list of selected items in the QListView in QT 4.6.?

    QFileSystemModel provides simple interface to operate on local file system. If you connect this with a List View I think it will sort out your problem.

Similar Threads

  1. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 13:49
  2. Replies: 6
    Last Post: 27th March 2010, 06:42
  3. QListview set selected item
    By Freeman551 in forum Qt Programming
    Replies: 1
    Last Post: 25th December 2009, 01:17
  4. Replies: 5
    Last Post: 2nd April 2007, 09:57
  5. Remove selected QListWidgetItem
    By xgoan in forum Newbie
    Replies: 7
    Last Post: 28th September 2006, 15:42

Tags for this Thread

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.