View Full Version : How can I remove a list of selected items in the QListView in QT 4.6.?
Leviathan
24th July 2010, 13:55
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:
QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
foreach(QModelIndex index, indexes)
{
model->removeRow(index.row());
}
removeRows also not suitable, it removes N-items that follows the one given.
I use QStandardItemModel to store items.
Lykurg
24th July 2010, 16:11
Something like that should work. (Idea: sort the list and begin at the end to ensure that the indices stay valid):
YOURVIEW->setUpdatesEnabled(false);
QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
indexes.sort();
for (i = indexes.count() - 1; i > -1; --i)
model->removeRow(indexes.at(i).row());
YOURVIEW->setUpdatesEnabled(true);
Leviathan
25th July 2010, 07:12
Here's the solution:
QModelIndexList indexes = ui.listview_files->selectionModel()->selectedIndexes();
while(indexes.size()) {
model->removeRow(indexes.first().row());
indexes = ui.listview_files->selectionModel()->selectedIndexes();
wysota
25th July 2010, 10:51
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.
saa7_go
26th July 2010, 05:02
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.
ui->listView->setUpdatesEnabled(false);
QModelIndexList indexes = ui->listView->selectionModel()->selectedIndexes();
qSort(indexes.begin(), indexes.end());
for(int i = indexes.count() - 1; i > -1; --i)
model->removeRow(indexes.at(i).row());
ui->listView->setUpdatesEnabled(true);
And that is work. Thanks for your sugesstion, wysota.
wysota
26th July 2010, 08:52
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.
Lykurg
26th July 2010, 09:26
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.
Bloody
13th September 2012, 12:10
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?
wysota
13th September 2012, 13:18
What is the "removing-thing"?
Bloody
14th September 2012, 08:24
ui->listView->setUpdatesEnabled(false);
QModelIndexList indexes = ui->listView->selectionModel()->selectedIndexes();
qSort(indexes.begin(), indexes.end());
for(int i = indexes.count() - 1; i > -1; --i)
model->removeRow(indexes.at(i).row());
ui->listView->setUpdatesEnabled(true);
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. :)
wysota
14th September 2012, 08:49
So what is the problem? Make a signal-slot connection and put your code in a custom slot of yours.
Bloody
14th September 2012, 08:58
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.
wysota
14th September 2012, 10:38
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.
Bloody
1st October 2012, 11:29
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?
ZikO
2nd October 2012, 03:10
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.