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. QSqlTabelModel - QTableView - insertRecord not editable [solved]
QtWS25 Last Chance

QSqlTabelModel - QTableView - insertRecord not editable [solved]

Scheduled Pinned Locked Moved General and Desktop
qsqltablemodelqtableview
5 Posts 2 Posters 2.0k Views
  • 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.
  • A Offline
    A Offline
    Andy314
    wrote on 16 Jun 2015, 10:41 last edited by Andy314
    #1

    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);
    }
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      Andy314
      wrote on 17 Jun 2015, 09:47 last edited by
      #2

      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 ?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on 17 Jun 2015, 12:36 last edited by ChrisW67
        #3

        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.

        A 1 Reply Last reply 19 Jun 2015, 09:53
        0
        • C ChrisW67
          17 Jun 2015, 12:36

          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.

          A Offline
          A Offline
          Andy314
          wrote on 19 Jun 2015, 09:53 last edited by
          #4

          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 ?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 20 Jun 2015, 06:59 last edited by
            #5

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

            1 Reply Last reply
            0

            5/5

            20 Jun 2015, 06:59

            • Login

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