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



  • 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?



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


Log in to reply
 

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