Unite data from two QSqlRelationTableModel into one TableView



  • 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?



  • @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?



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • 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



  • 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



  • 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;
    


  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.