PDA

View Full Version : Remove item from QListView's model



janck
18th June 2013, 16:47
Hello!

I'm trying to remove one of few listed files from model of QListView. I'm using this code but it doesn't work:

ui->listView->model()->removeRow(ui->viewFiles->currentIndex().row());
Same thing is if I use model->removeRow(..same as above..);

What's wrong with this code and how to delete item from list?

Thank you!

anda_skoa
18th June 2013, 18:13
If this is a QFileSystemModel then it has a remove method that takes a model index and removes the file represented by that model index.
Seems removeRow() does not change the files on disk, thus not changing the actual data the model works on.

Cheers,
_

ChrisW67
18th June 2013, 23:41
How are the models underlying ui->listView and ui->viewFiles related? Are they pointing at the same model? Are there proxy models involved? What type is the underlying model?

janck
19th June 2013, 00:04
@anda_skoa: I don't want to write changes (to delete file) on disk but only in application's list.
@ChrisW67:
ui->listView = ui->viewFiles (I was just testing out few commands which I found on StackOverflow and forget to change viewFiles to listView)
What are proxy models? :D
If underlying model means model which is set to listView than it's type is QFileSystemModel. As I said, I just want to remove item from list not from disk.

ChrisW67
19th June 2013, 02:37
QFileSystemModel does not implement removeRows() (which removeRow() uses). It does have remove() but that actually removes the file or directory from the file system. One option is to hide the relevant rows in the view with QListView::setRowHidden(). Another option is to use a QSortFilterProxyModel between the QFileSystemModel and the view and filter out the rows that are not to be shown.