Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Inserting an empty row into a proxy model impede submitting the data to the database.

Inserting an empty row into a proxy model impede submitting the data to the database.

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 3.4k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    Edico
    wrote on last edited by
    #1

    I have something like that:
    @
    model = new QSqlRelationalTableModel(this);
    model->setTable("Produse");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();

    proxyModel = new QSortFilterProxyModel;
    proxyModel->setSourceModel(model);
    proxyModel->setFilterKeyColumn(model->fieldIndex("Denumire"));
    proxyModel->insertRow(0);
    
    int newRow = proxyModel->rowCount();
    proxyModel->insertRow(newRow);
    proxyModel->setData(proxyModel->index(newRow, 1), "text");
    proxyModel->setData(proxyModel->index(newRow, 2), 123);
    ...
    
    model->submitAll();
    

    @

    The problem is that after submitAll(), the data isn't written to the database.
    Without proxyModel->insertRow(0) it works fine, the data is written to the database, but I need an empty row.

    If I do:
    @
    ...
    proxyModel->insertRow(0);

    int newRow = 0;
    proxyModel->insertRow(newRow);
    ...
    @
    it works. Why?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Edico
      wrote on last edited by
      #2

      A solution I've found is to remove the empty row inserted at the begining (0), add the new record to the database and then insert again the empty row at the begining of the model.
      Something like that:
      @
      model = new QSqlRelationalTableModel(this);
      model->setTable("Produse");
      model->setEditStrategy(QSqlTableModel::OnManualSubmit);
      model->select();

      proxyModel = new QSortFilterProxyModel;
      proxyModel->setSourceModel(model);
      proxyModel->setFilterKeyColumn(model->fieldIndex("Denumire"));
      proxyModel->insertRow(0);

      proxyModel->removeRow(0);
      int newRow = proxyModel->rowCount();
      proxyModel->insertRow(newRow);
      proxyModel->setData(proxyModel->index(newRow, 1), "text");
      proxyModel->setData(proxyModel->index(newRow, 2), 123);
      ...

      model->submitAll();
      proxyModel->insertRow(0);
      @

      But this is awkward.

      1 Reply Last reply
      0

      • Login

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