Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qtableview add rows based on other Qtableview



  • I have two table table1 and table2
    and in table1 i have option to add and del row so in table 2 i want rows as same as in table1 as if row added /del in table1 it should update in table2 as well
    how to do this ?
    thanks in advance



  • connect(ui.tableView_1->model(), &QAbstractItemModel::rowsInserted, ui.tableView_2->model(),[&](const QModelIndex& parent, int first, int last){ui.tableView_2->model()->insertRows(first,1+last-first,parent);});
    and so on



  • You just need to connect a slot to table1,&QAbstractItemModel::rowsRemoved/table1,&QAbstractItemModel::rowsInserted that deletes/inserts the coresponding row in table2



  • @VRonin said in Qtableview add rows based on other Qtableview:

    QAbstractItemModel::rowsRemoved

    you mean using removeRow()and insertRow ?
    bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex())
    bool QAbstractItemModel::insertRow(int row, const QModelIndex &parent = QModelIndex())



  • you mean using removeRow()and insertRow ?

    indeed



  • @VRonin said in Qtableview add rows based on other Qtableview:

    like this ??

    connect(ui.tableView_1->model(), &QAbstractItemModel::rowsInserted, ui.tableView_1->model(), [ui.tableView_1- >model()](const QModelIndex& parent, int first, int last)
    QAbstractItemModel* table1 = ui.tableView_1->model();
    QAbstractItemModel* table2 = ui.tableView_2->model();
    connect(table1, &QAbstractItemModel::rowsRemoved, table2, [table2](const QModelIndex& parent, int first, int last)


  • connect(ui.tableView_1->model(), &QAbstractItemModel::rowsInserted, ui.tableView_2->model(),[&](const QModelIndex& parent, int first, int last){ui.tableView_2->model()->insertRows(first,1+last-first,parent);});
    and so on


Log in to reply