How to deal with dataChanged from TableView Model signal



  • I have a TableView with a Model, i can double-click to edit cells and i need to get the edit stuff to update in the BDD.

    I've got the following that is triggered after enter:

    connect(ui->tableViewQso->model(),
            SIGNAL(dataChanged(QModelIndex,QModelIndex)), this,
            SLOT(onUpdateQso(const QModelIndex&, const QModelIndex&)));
    ...
    void qthLogMW::onUpdateQso(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
         statusLabel->setText(tr("Asked for update"));
    }
    

    How i could get edit stuff ? I can't found what to do with topLeft & botRight.


  • Moderators

    You are not supposed to connect to dataChanged from outside. I mean you of course can and in some cases that's useful, but that is not how model/view in Qt works.
    When the edit finishes a setData of the model is called. In there you should set the data in whatever storage for it you have and if it succeeded emit dataChanged and return true.
    In the simplest form dataChanged will have topLeft and bottomRight values pointing to the same index - the one that was just edited and passed to setData.
    If there is some data dependency going on, where a change of one value causes others to change too, you can emit dataChanged with a "rectangular" region indicated by topLeft and bottomRight that contains the changed values.
    In response to dataChanged the view will invalidate the indicated region and query the model with its data() member for the changed values.



  • @Chris-Kawa thanks, i now see the thing.
    I've found example http://doc.qt.io/qt-5/modelview.html#2-5-the-minimal-editing-example but it use QAbstractTableModel and i'm using a QSqlRelationalTableModel instead of QSqlTableModel.

    Will it still works for relations ? I can't found an Abstract for RelationalTableModel.



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