Solved Filtering QSqlTableView data
-
Hi,
For this use case, you could add a QSortFilterProxyModel.
So you can filter the loaded data without touching the SQL filters.
-
@SGaist said in Filtering QSqlTableView data:
Hi,
For this use case, you could add a QSortFilterProxyModel.
So you can filter the loaded data without touching the SQL filters.
Thank you for your suggestion. It works like a charm :-)
-
I have managed to present students data using QSortFilterProxyModel, but I am having some problems with it.
When I open a page that lists all the students I call part of the code:localStudentModel = hStudents->getStudents(); proxyModelStudents->setSourceModel(localStudentModel); proxyModelStudents->setDynamicSortFilter(true); ui.tableViewStudents->setModel(proxyModelStudents);
localStudentModel is QSqlRelationalTableModel pointer. Everything works the first time that I open this screen. It is stacked widget type of the screen so I need to move between screen for other purposes.
If I click on the students button again I need to reload students table because there is a possibility that I have inserted new rows into students table so I call the same part of the code again.
This time application crashes with debug assertion failure.ASSERT: "source_to_proxy.size() > end" in file itemmodels\qsortfilterproxymodel.cpp, line 1282
Debug Error!I am not sure what might be a problem.
Tnx
-
What is that localStudentModel ?
-
@SGaist
QSqlRelationalTableModel *localStudentModel;QSqlRelationalTableModel* Students::getStudents()
-
You are creating a completely new model each time ?
-
Well no. In my homescreen constructor I once create new object
hStudents = new Students();
and this object returns QSqlRelationalTableModel when I call getStudents() , which I assign to existing localStudentModel pointer.
-
Ok, so what exactly happens when you click on that "students button" ?
-
Well I open specific stack widget page, query "student" table and return all data to localStudentModel. Then I setup proxymodelStudents whit that model and assign that proxyModelStudent to QTableView that is located on that stackwidget page.
After that I just hide some columns and organize that qtableview. This all works first time I open the page.
Second time I click on students button program crashes with this message:And one more thing. Before I added proxyModel this all worked.
-
But why not just refresh the model query ? From your description it looks like you are re-creating the whole stuff each time you click on that button.
-
@SGaist I think that you are right. I modified the code and now I just refresh a model. It works. Thank you.
-
Just update the query of your model.