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. Strange things happening to QSqlTableModel (while inserting, deleting, updating)
Qt 6.11 is out! See what's new in the release blog

Strange things happening to QSqlTableModel (while inserting, deleting, updating)

Scheduled Pinned Locked Moved General and Desktop
qsqltablemodel
1 Posts 1 Posters 584 Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    The code:

    model = new QSqlTableModel(this, db1);
    model->setTable("subjects");
    model->setEditStrategy(QSqlTableModel::OnFieldChange);
    model->select();
    model->removeColumn(0); // don't show the ID
    ui->tableView->setModel(model);
    

    Inserting:

    model->insertRow(0);
    

    Deleting:

    QModelIndexList selection = ui->tableView->selectionModel()->selectedRows();
    if (selection.count())
    {
        for (int i = 0; i < selection.count(); i++)
        {
            QModelIndex index = selection.at(i);
            qDebug() << index.row();
            qDebug() << model->removeRow(selection.at(i).row());
            model->select();
            qDebug() << model->lastError();
           //model->submitAll();
        }
        //model1->submitAll(); 
    }
    

    So, the table displays correctly.
    When I edit a cell, the row gets empty and shows !, like it was deleted. Nothing changes in DB.
    When I delete a row with algorithm above, removeRow returns true, the table view blinks like it's re-rendering, but nothing changes.
    When I insert a row, edit it, it gets empty and shows !, BUT it appears in DB and it's in the table when I reopen the app.
    When I un-comment model->submitAll(); nothing changes.
    Any ideas?

    1 Reply Last reply
    0

    • Login

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