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. Unite data from two QSqlRelationTableModel into one TableView
QtWS25 Last Chance

Unite data from two QSqlRelationTableModel into one TableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlmodeldatamodelview
8 Posts 3 Posters 3.2k Views
  • 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.
  • S Offline
    S Offline
    SergeyK12
    wrote on last edited by
    #1

    Hi,
    is there any way to unite data from TableModel #1 with data from TableModel #2 in a TableView?
    Ofcource i could write them from one model to another, but i need to change data in TableModel #2. (Is it possible to write them back, but its seems to be wrong way).
    I though that it could be done by writing my own successor of QAbstractProxyModel, but i dont succeed with this.
    Is it a proper way or there is something else?

    VRoninV 1 Reply Last reply
    0
    • S SergeyK12

      Hi,
      is there any way to unite data from TableModel #1 with data from TableModel #2 in a TableView?
      Ofcource i could write them from one model to another, but i need to change data in TableModel #2. (Is it possible to write them back, but its seems to be wrong way).
      I though that it could be done by writing my own successor of QAbstractProxyModel, but i dont succeed with this.
      Is it a proper way or there is something else?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @SergeyK12 said in Unite data from two QSqlRelationTableModel into one TableView:

      I though that it could be done by writing my own successor of QAbstractProxyModel

      It can't as QAbstractProxyModel only supports 1 source model, you'd have to go one step back and reimplement QAbstractItemModel

      @SergeyK12 said in Unite data from two QSqlRelationTableModel into one TableView:

      is there any way to unite data from TableModel #1 with data from TableModel #2

      Define "unite". append the rows of TableModel #2 to TableModel #1? put the columns side by side?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SergeyK12
        wrote on last edited by
        #3

        I mean - put the columns side by side.
        Table #1 consist of parameter name, unit etc. and its value is in Table #2. Im trying to show them in one TableView (and it seems like i must think about this strange logic)
        So, it is impossible to add additional model in successor of QAbstractProxyModel by my own as a private field and the setter for it?
        I will try to use QAbstractItemModel

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

          Hi,

          Why about making a QSqlQueryModel with the query to build that data the way you want it ?

          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
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            If you do not need the changes in the models to feed back into the database just use QSqlQueryModel as @SGaist suggested and let SQL do the merging for you.

            If you need the changes to go in the DB then probably the fastest way is using KExtraColumnsProxyModel and link the extra columns to the TableModel #2

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • S Offline
              S Offline
              SergeyK12
              wrote on last edited by
              #6

              Thanks for your request.
              Since i need to change data inside DB i suppose i should use KDE (but its too hard to build a library).
              What should i do to write ny own proxy with such property?
              I almost write one, it could show me data but data from #2 table is in gray mode (i couldnt change them, and there is a rectangle inside the cell screenshot).

              I reimplement the following method of QIdentityProxyModel:

              • data, index, columnCount
              • add a private field with QSqlRelationTableModeland the setter.

              I suppose there is something alse which i should reimplement to make field in 5 cell editebel. SetData ofcource, but why is it in a gray mode? There is a method which make cells editable?

              Thanks

              VRoninV 1 Reply Last reply
              0
              • S Offline
                S Offline
                SergeyK12
                wrote on last edited by SergeyK12
                #7

                Here is my methods

                    int columnCount(const QModelIndex &) const
                    {
                        return sourceModel()->columnCount()+1;
                    }
                    QVariant data(const QModelIndex &ind, int role) const
                    {
                        if(ind.column() < 4)
                            return sourceModel()->data(mapToSource(ind), role);
                        else
                            return m_model->data(m_model->index(m_id, ind.row()+1));
                    }
                    void setExtraColumnModel(int id, QSqlRelationalTableModel *model)
                    {
                        m_id = id;
                        m_model = model;
                    }
                
                private:
                    int m_id;
                    QSqlRelationalTableModel *m_model;
                
                1 Reply Last reply
                0
                • S SergeyK12

                  Thanks for your request.
                  Since i need to change data inside DB i suppose i should use KDE (but its too hard to build a library).
                  What should i do to write ny own proxy with such property?
                  I almost write one, it could show me data but data from #2 table is in gray mode (i couldnt change them, and there is a rectangle inside the cell screenshot).

                  I reimplement the following method of QIdentityProxyModel:

                  • data, index, columnCount
                  • add a private field with QSqlRelationTableModeland the setter.

                  I suppose there is something alse which i should reimplement to make field in 5 cell editebel. SetData ofcource, but why is it in a gray mode? There is a method which make cells editable?

                  Thanks

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @SergeyK12 said in Unite data from two QSqlRelationTableModel into one TableView:

                  I reimplement the following method of QIdentityProxyModel:

                  data, index, columnCount
                  add a private field with QSqlRelationTableModeland the setter.

                  I suppose there is something alse which i should reimplement to make field in 5 cell editebel. SetData ofcource, but why is it in a gray mode? There is a method which make cells editable?

                  You can't have a proxy model with 2 source models. internally it will rely on mapFromSource and mapToSource which won't work in your case, start directly from QAbstractItemModel.

                  The methods you reimplemented are fine, you miss rowCount(), setData() and flags()


                  use KDE (but its too hard to build a library).

                  no it's not... detailed instructions to build any tier 1 KDE module (spoiler alert: it's only 9 cmd lines): https://forum.qt.io/topic/76533/copy-selected-rows-of-a-table-to-a-model/12

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  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