Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Filtering QSqlTableView data

Filtering QSqlTableView data

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ZgemboZ Offline
    ZgemboZ Offline
    Zgembo
    wrote on last edited by Zgembo
    #1

    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:
    localGradesModel->setFilter("");

    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?

    Tnx,
    Zgembo

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      For this use case, you could add a QSortFilterProxyModel.

      So you can filter the loaded data without touching the SQL filters.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ZgemboZ 1 Reply Last reply
      4
      • SGaistS SGaist

        Hi,

        For this use case, you could add a QSortFilterProxyModel.

        So you can filter the loaded data without touching the SQL filters.

        ZgemboZ Offline
        ZgemboZ Offline
        Zgembo
        wrote on last edited by
        #3

        @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 :-)

        1 Reply Last reply
        0
        • ZgemboZ Offline
          ZgemboZ Offline
          Zgembo
          wrote on last edited by Zgembo
          #4

          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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What is that localStudentModel ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            ZgemboZ 1 Reply Last reply
            0
            • SGaistS SGaist

              What is that localStudentModel ?

              ZgemboZ Offline
              ZgemboZ Offline
              Zgembo
              wrote on last edited by
              #6

              @SGaist
              QSqlRelationalTableModel *localStudentModel;

              QSqlRelationalTableModel* Students::getStudents()

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You are creating a completely new model each time ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • ZgemboZ Offline
                  ZgemboZ Offline
                  Zgembo
                  wrote on last edited by Zgembo
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Ok, so what exactly happens when you click on that "students button" ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • ZgemboZ Offline
                      ZgemboZ Offline
                      Zgembo
                      wrote on last edited by Zgembo
                      #10

                      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:

                      0_1559418684738_3380d282-ef19-48e7-9b98-f3d48d1e2711-image.png

                      And one more thing. Before I added proxyModel this all worked.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        ZgemboZ 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          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.

                          ZgemboZ Offline
                          ZgemboZ Offline
                          Zgembo
                          wrote on last edited by Zgembo
                          #12

                          @SGaist I think that you are right. I modified the code and now I just refresh a model. It works. Thank you.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Just update the query of your model.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved