Empty selection model after introducing QSqlTableModel->filter()
-
Good afternoon,
I can't retrieve selected items after filtering the model. Perhaps someone can shed some light on the possible solution. Part of my app is built as follows:SqLite db;
QSqlTableModel reads from db.
QTableView presents the data from model to the user.User can select rows (on or many) and then print them (single QPushButton).
So far, it worked for over 2y. Recently I've been asked to introduce filtering, to be able to display only those rows that certain column, let's name it "type" has specified value or all the columns. What I did:
QComboBox gathering all the possible live values from column "type" from db plus single option "no filter".
As manual for QSqlTableModel states, filter property is for that, so I included slot (docs is of type QSqlTableModel):void MainWindow::applyItemFilter(QString filter) {
if (filter == "No filter") {
docs->setFilter("type is not null");
} else {
docs->setFilter("type = '"+filter+"'");
};
}Display works perfectly well but whenever I try to retrieve selected items via:
for (int x = 0; x<ui->items->selectionModel()->selectedRows().count();x++) { code in here; }
the count(); is equal to zero regardless of selection.
What have I missed?
Kind Regards,
Artur Wawrowski -
Hi,
IIRC, setFilter will reload the data since it does a query to the database.
Two solutions comes to mind:
- Use QSortFilterProxyModel.
- Store your selected data and once the filter is applied, re-select the data that are still available.
-
Hi,
IIRC, setFilter will reload the data since it does a query to the database.
Two solutions comes to mind:
- Use QSortFilterProxyModel.
- Store your selected data and once the filter is applied, re-select the data that are still available.
@SGaist Yes, invoking setFilter() implies select() and filter is basically (according to docs and verified by me just throwing out last query from the model after the filter is aplied) adding "where" clause the the query done by the model. That works as describes in documentation and I have no complaints whatsoever.
But somehow the fact that I apply filter makes selectionModel in the view empty. I can try with QSortFilterProxyModel indeed, in fact I use it with a great success to provide fast search in different part of the program... I was just wondering if I missed something as documentation does not imply any sort of limitations with regards to QTableView and model utilizing a filter property. It is bizarre. If that is the only suggestion I will try it out tomorrow, luckily there is not much of change in the code to do that.
I'll post the results in here. Thank you.
-
Hi,
IIRC, setFilter will reload the data since it does a query to the database.
Two solutions comes to mind:
- Use QSortFilterProxyModel.
- Store your selected data and once the filter is applied, re-select the data that are still available.
@SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.
-
@SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.
@artwaw said in Empty selection model after introducing QSqlTableModel->filter():
@SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.
I'm not sure I'm following you on that one. How would you be saving the current selection ?
-
@artwaw said in Empty selection model after introducing QSqlTableModel->filter():
@SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.
I'm not sure I'm following you on that one. How would you be saving the current selection ?
-
Hi,
IIRC, setFilter will reload the data since it does a query to the database.
Two solutions comes to mind:
- Use QSortFilterProxyModel.
- Store your selected data and once the filter is applied, re-select the data that are still available.
-
No worries !
Since you feel there's something missing, you can submit an improvement to the documentation and doing so make it available to other developers :)