E
E
Evgeny Markov2021-08-17 20:16:03
C++ / C#
Evgeny Markov, 2021-08-17 20:16:03

How to reflect row change/deletion from QSqlRelationTableModel in SQLite DB?

I am using a QTableView in conjunction with a QSortFilterProxyModel, a QSqlRelationalTableModel and a FlipProxyDelegate delegate (parent of the QSqlRelationalDelegate). Changing or deleting a row in a QTableView is not reflected in the database.

Tried to use QTableView with QSqlRelationalTableModel and QSqlRelationalDelegate didn't work

. Model creation code snippet:

QSqlRelationalTableModel * tableModel = new QSqlRelationalTableModel;
tableModel->setTable( currentTable );
tableModel->setEditStrategy( QSqlRelationalTableModel::OnManualSubmit );

createRelations( tableModel, currentTable );
    
filterView->setItemDelegate( new FlipProxyDelegate(tableModel) );
tableModel->select();
    
sortModel->setSourceModel( tableModel );
filterView->setModel( sortModel );

Transaction confirmation:

void InterfaceChanger::submit()
{
    FilterTableView *filterView = (FilterTableView*)ui->layout->itemAt(0)->widget();
    QSortFilterProxyModel * sortModel = (QSortFilterProxyModel *)filterView->getModel();
    QSqlTableModel *model = (QSqlTableModel *)sortModel->sourceModel();
    model->database().transaction();
    if (model->submitAll())
    {
        model->database().commit();
        model->select();
    }
    else
    {
        model->database().rollback();
        QMessageBox::critical( nullptr,
                                             "Ошибка",
                                             model->lastError().databaseText());
    }
    
}


Project source code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Markov, 2021-08-18
@Mark194

Didn't set primary key for tables containing foreign keys

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question