Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Inconsistency between model and view after removing a row from a table
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlmodel view progmodel-view
2 Posts 2 Posters 453 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.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #1
    This post is deleted!
    sierdzioS 1 Reply Last reply
    0
    • AhtiA Ahti

      This post is deleted!

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved