Question

1. I'm trying to remove one of a few listed files from a QListView model. 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(ui->viewFiles->currentIndex().row());

What's wrong with this code, and how do I delete an item from the list?

2. QFileSystemModel loads only files from the selected directory. How do I count all those items from QListView's model?

Was it helpful?

Solution

  1. removeRow(int row) should normally works. Make sure that ui->viewFiles->currentIndex().row() returns a valid row (i.e., between 0 and model->rowCount()-1). Notably, it is possible that your QModelIndex ui->viewFiles->currentIndex() is in an invalid state. You can check it via ui->viewFiles->currentIndex().isValid().

  2. As said, you can check the number of rows of your model via model->rowCount()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top