Important: Please read the Qt Code of Conduct -

Filtering QSqlTableView data

  • Hi all,

    I am working on a project which include data presentation from the database. I have created an QSqlRelationalTableModel model and I have added that model to QSqlTableView. This part works OK. I have loaded data into model using some constraints, for example I am showing only students that are enrolled in specific course.
    I have created an QLineEdit which accepts in this example student names and filters this view in this way:
    localStudentModel->setFilter(QString("fName LIKE '%1%'").arg(ui.lineEditFName->text()));

    I have connected this QLineEdit via signal and slot mechanism and each time I type a letter into line edit model is filtered using above statement. Problem occurs when I delete all the letters that I have entered into this lineedit.
    In order to remove a filter I use this statement:

    When this statement executes I not only get students from specific course, but all the students that are stored in the students table, which of course is not something that I want.

    Is there a better way to filter already presented data in QTableView?


  • Lifetime Qt Champion


    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:


    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();

    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.


  • Lifetime Qt Champion

    What is that localStudentModel ?

  • @SGaist
    QSqlRelationalTableModel *localStudentModel;

    QSqlRelationalTableModel* Students::getStudents()

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    Just update the query of your model.

Log in to reply