QSqlTabelModel - QTableView - insertRecord not editable [solved]



  • When I insert a new record in a SqlTableModel, I can see this new row in the table, with empty fields but I cannot edit it. The row marker gets a "!".
    Editing for old records works correctly. When I refresh the TableModel (after SubmitAll) I see this new record.

    So my question why are the new fields empty and how to edit a just inserted record ?

    void Lession_Dlg::on_NewBtn_clicked()
    {
        QSqlRecord r = tblLession->record();
        r.setGenerated(0, false);           // auto primary key
        r.setValue("IDCategory", IDCat);
        r.setValue("LessionName", "x");
        tblLession->insertRecord(0, r);
        ui->Lessions->resizeRowsToContents();
        QModelIndex i = tblLession->index(0, 2);
        ui->Lessions->setCurrentIndex(i);
        ui->Lessions->edit(i);
    }
    


  • I found out that the problem is the auto_increment_primary_key once again. It seem not possible to edit a new record in the grid (like in a MSAccess table).

    In an other thread here I had as similar problem. I tried to get back the primary key from an InsertRecord action in an QSqlTableModel. I thought this was solved, but it worked only by random sometimes. The problem is that after QSqlTable::subimtAll() the new record changes it position and I dont know where I can read it back.

    I found a lot of threads in the internet about this topic but nowhere a really compatible solution for it. Why is this common task in Qt so complicated ? What is the conclusion ?

    The auto-primary-key functionality is not is not really possible in Qt ?
    Do never use it in Qt ?

    Must I implement my own multi-user primary-key mechanism ?



  • This is how I managed something similar:

    • When you insertRecord() you do so at a known record number.
    • Fetch the new record at that record number. It has not moved.
    • Store the allocated unique ID from the record (qint64 or the QVariant)
    • Do whatever it is you are doing that reorders the rows.
    • Find the inserted row using the allocated unique ID using something like:
    QModelIndex idCol = tableModel->index(0, 0);
    QModelIndexList indexes = tableModel->match(idCol, Qt::EditRole, QVariant(id), 1, Qt::MatchExactly);
    // desired row is the first entry in indexes (if any found);
    

    You may find that turning off sorting in views while you do the insert helps. It will stop the view moving the inserted row and getting confused.



  • Hi @ChrisW67 thank you for your answer.

    Here what I found out:

    The new record does not move after submitAll if no sorting and really no filter is applied.
    I had in my test-code Model.setFilter(""); This should do nothing but it changes indeed the position of the new record. Has the TabelModel a function for really disabling the filter ?

    In my other example I need a tableview for the model and a sorting and filtering. Here is it not possible to get the new PK! So the way to edit a new inserted record is:
    Save the record in an independent TableModel, getPK, refresh the TableView and jump to the new record.
    An other way is, because in this example I dont really need the new PK, insert a record at a specific position without SubmitAll() and jump to the new row. For this the model must be in ManualSubmit mode which I dont like. I prefer RowChange or CellCange saving.
    I tried switch to ManualSubmit -> Insert Record -> jump to it -> RowChangeSubmit.
    Bit this does not work. RowChangeSubmit performs a SubmitAll instantly and I dont know where the new record is.
    Has somebody a solution or must I take always ManualSubmit ?



  • An empty string should disable the filter. However, if the underlying select it active then it will trigger calling select() which resets the model.


Log in to reply
 

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