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. SQL table view - removed element stays on the view

SQL table view - removed element stays on the view

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.2k 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.
  • J Offline
    J Offline
    Jendker
    wrote on last edited by
    #1

    Hello everyone!

    I am having a problem with the presentation of the data with QSql. I am using the default QSQLITE driver.

    I created the button to remove the element in the TableView and everything was working fine, till I used the line:

    sql_model_->setEditStrategy(QSqlTableModel::OnManualSubmit);
    

    After removing it, so that the changes to the database are actually saved to the database file on disk I am having such an effect after removing the element:

    0_1524002536212_Capture.PNG

    Here the first element should have been removed. The function removeRow is returning true. After the restart of the application the object is gone (as it should):
    0_1524002656996_Capture2.PNG

    The code for the remove slot. As said earlier the function 'removeRow' returns true:

    void DialogOpenArticle::remove()
    {
        auto selected_index_list = ui->tableView->selectionModel()->selectedIndexes();
        if(selected_index_list.empty())
        {
            QMessageBox::information(0, tr("Error"), tr("No elements selected. Please retry."));
            return;
        }
        auto index = selected_index_list.at(0);
        model_->removeRow(index.row());
    }
    

    And the constructor of the dialog:

    DialogOpenArticle::DialogOpenArticle(QSqlRelationalTableModel *model, QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DialogOpenArticle),
        model_(model)
    {
       /* ... setup of the irrelevant part of the program ... */
    
        ui->tableView->setModel(model_);
        ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
        ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
    

    When I am inserting a new element with 'insertRecord' the change is immediately correctly reflected in the table view. Am I missing some update function? Maybe I could try with a different driver for database, but which one is included also by default so that I can avoid the hassle with the building of the driver just for the test? That would be nice :)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      So this happens with deletes until you call submitAll(). What you can do is hide the row after the delete or just call submitAll and the model will update properly.

      This is a feature of sql models only and doesn't happen with other memory based models. So of course you could put your sql model into a memory mapped one and that should work as well. That's more work than it's worth though, easiest just to hide the deleted rows until things are committed. Or force the commit with submitAll().

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      3
      • J Offline
        J Offline
        Jendker
        wrote on last edited by Jendker
        #3

        Thank you for clearing that up! I was a bit concerned because I have never seen in any example that someone was doing any submit or row hiding. I will check it in the evening and mark the topic as solved.

        EDIT: submitAll did not help in this case, but the usual hiding of the row solved the issue :) The only thing which may be a drawback of this solution is that the numbering of the rows in the table view stays as it was before hiding, so that some numbers are missing. For me luckily it's not a problem.

        1 Reply Last reply
        1

        • Login

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