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

Inconsistency between model and view after removing a row from a table



  • This post is deleted!

  • Moderators

    @Ahti said in Inconsistency between model and view after removing a row from a table:

    JsonObject u_data;
    u_data.insert("id", model->record(index.row()).value(0).toInt());
    u_data.insert("name", model->record(index.row()).value(1).toString());
    if (role == Qt::DisplayRole)
    return u_data;

    That's inefficient. If you only support DisplayRole, then you can move that QJsonObject directly into that if statement.

    if (role == Qt::DisplayRole) {
      QJsonObject u_data;
      u_data.insert("id", model->record(index.row()).value(0).toInt());
      u_data.insert("name", model->record(index.row()).value(1).toString());
      return u_data;
    }
    

    Second, you should use the parent here, even if it is a flat list:

    beginRemoveRows(QModelIndex(), first, last);
    response = model->removeRow(first, QModelIndex());

    beginRemoveRows(parent, first, last);
    response = model->removeRow(first, parent);
    

    But why you get this strange behavior when removing - I don't know. Perhaps it has something to do with the fact that you have two models here - UseModel is a list model and your model variable is a QSqlTableModel (which inherits from the same base class as list model). Perhaps these calls get duplicated, although it is unlikely.

    Try adding some qDebug() calls into removeRows(), or run it through a debugger, to see what is happening.