Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Proxy Model for Combining Table Models
Forum Updated to NodeBB v4.3 + New Features

Proxy Model for Combining Table Models

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
11 Posts 4 Posters 1.2k 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.
  • F Offline
    F Offline
    Flick
    wrote on last edited by Flick
    #1

    Hello. I have two SQLite tables being used as QAbstractTableModel objects in C++ code. I want to use these two models as sources of truth for a new TableView that will display a combination of the two table models joined together.

    How can I use a proxy model to combine data from two or more other models, and keep the new model in sync with both parent models? Are there good tutorials or examples anywhere?

    Model 1

    id data1 data2 data3 other_id

    Model 2

    id (other_id) data3 data4

    Model 3 (goal)

    id other_id data1 data2 data3 data3 data4

    When I filter either model 1 or model 2, I want model 3 to update.

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

      Hi,

      How do you want to assemble these data ?
      Is it some sort of join ? What are the join points ?

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

      F 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        How do you want to assemble these data ?
        Is it some sort of join ? What are the join points ?

        F Offline
        F Offline
        Flick
        wrote on last edited by
        #3

        @SGaist Yes, I'm essentially joining the two tables where Model1.other_id = Model2.id and wanting my combined view to update based on changes to either base model.

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

          How are the base models updated ?

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

          F 1 Reply Last reply
          0
          • SGaistS SGaist

            How are the base models updated ?

            F Offline
            F Offline
            Flick
            wrote on last edited by Flick
            #5

            @SGaist Each model (model1, model2) is its own QAbstractTableModel made to read data w/ QSQLQuery commands. These models handle

            1. SQL role name extraction
            2. SQL filtering / sorting
            3. item selection (QItemSelectionModel)

            We get data through the last ran QSQLQuery* _query, and re-query whenever sorting, filtering, or CRUD operations are applied with the same _query object. My issue is, I can only setModel once for QML views like TableView and I'm not sure how to base one view on two models.

            JonBJ 1 Reply Last reply
            0
            • F Flick

              @SGaist Each model (model1, model2) is its own QAbstractTableModel made to read data w/ QSQLQuery commands. These models handle

              1. SQL role name extraction
              2. SQL filtering / sorting
              3. item selection (QItemSelectionModel)

              We get data through the last ran QSQLQuery* _query, and re-query whenever sorting, filtering, or CRUD operations are applied with the same _query object. My issue is, I can only setModel once for QML views like TableView and I'm not sure how to base one view on two models.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Flick
              There is nothing out-of-the-box for JOINing Qt models once data read into them. Let alone that I have no idea what restrictions QML might impose. You would have to write code to do that.

              Since you seem to already re-query on any change in view (e.g. filtering, sorting), would you consider putting the JOIN at the SQL side (QSqlQuery), which is easy to do, and re-reading that too? It will be simpler than what you must add to do the join yourself on the QSql models in memory.

              1 Reply Last reply
              1
              • F Offline
                F Offline
                Flick
                wrote on last edited by
                #7

                I'm still lost. How would a sample TableView render two SQL tables as one model? Does anyone have an example?

                SGaistS 1 Reply Last reply
                0
                • F Flick

                  I'm still lost. How would a sample TableView render two SQL tables as one model? Does anyone have an example?

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Flick taking the join part aside for a second, you would need a proxy model that announces a number of columns matching the sum of columns of your two tables and the number of row of the biggest of the two tables. Then in the data function you would need to grab the data of the correct model when requested. Qt has such a model but just to concatenate rows of models.

                  Taking the join back in, you should really consider what @JonB wrote: create the model out of the query from the database. You database will be way better at doing the join than a model in code.

                  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
                  2
                  • F Offline
                    F Offline
                    Flick
                    wrote on last edited by
                    #9

                    If anyone could provide examples of 2+ joined SQL tables in a single QML model, I'd appreciate it. I can't use QSqlTableModel because it functions on just one table, but I would like all the functionality QSqlTableModel provides (hiding columns, sorting/filtering).

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #10

                      Use QSqlQueryModel and add the sorting/filtering there or with a QSortFilterProxyModel on top.

                      F 1 Reply Last reply
                      2
                      • GrecKoG GrecKo

                        Use QSqlQueryModel and add the sorting/filtering there or with a QSortFilterProxyModel on top.

                        F Offline
                        F Offline
                        Flick
                        wrote on last edited by
                        #11

                        @GrecKo This is what I'm working on implementing right now. Would there be a reason to use QAbstractTableModel over QSqlQueryModel?

                        1 Reply Last reply
                        0

                        • Login

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