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

QSqlTableModel: Updating record



  • What is the cleanest way to update in QSqlTableModel? I do something like this:

    QSqlTableModel adrTableModel;
    adrTableModel.setTable("address");
    adrTableModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    adrTableModel.setFilter(filter);
    adrTableModel.select();
    
    if (adrTableModel.rowCount() == 1) {
         QSqlRecord addressRecord = adrTableModel.record(0);
         addressRecord.setValue("isUsed", 1);
         adrTableModel.setRecord(0, addressRecord);
         adrTableModel.submitAll();
    }
    

    This works, but I am almost sure that there is an easier and cleaner way to update the record. What is the cleanest way to do it?



  • adrTableModel.setData(adrTableModel.index(row,column),"isUsed");
    adrTableModel.submitAll();
    

  • Lifetime Qt Champion

    Hi,

    Do you mean programmatically or through a GUI ?
    In the first case, there's nothing particularly wrong with what you wrote.

    You can also use setData.


Log in to reply