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. QSqlTableModel rowCount doesn't change after row delete
QtWS25 Last Chance

QSqlTableModel rowCount doesn't change after row delete

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.3k 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.
  • morteasherahM Offline
    morteasherahM Offline
    morteasherah
    wrote on last edited by
    #1

    I was developing a small qt quick app for clients management. I have encountered a problem that I have described here https://forum.qt.io/topic/67287/tableview-how-to-delete-a-row/2

    Investigating the problem, I have discovered that it is a bug in QSqlTableModel. I implemented the following code and ran it

    #include <QtSql/QSqlTableModel>
    #include <QDebug>
    #include <QtSql/QSqlDatabase>
    #include <QMessageBox>
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QString dbName(app.applicationDirPath());
        dbName.append("/mydb.sqlite");
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(dbName);
        if (!db.open()) {
            QMessageBox::critical(0, qApp->tr("Impossibile aprire il database"), "", QMessageBox::Cancel);
            return 0;
        }
    
        QSqlTableModel dataModel;
        dataModel.setTable("Clienti");
        dataModel.setEditStrategy(QSqlTableModel::OnRowChange);
        dataModel.select();
    
        while(dataModel.canFetchMore())
            dataModel.fetchMore();
    
        qDebug() << dataModel.rowCount();
        dataModel.removeRow(3);
        qDebug() << dataModel.rowCount();
    
        db.close();
    }
    
    

    As you can see, it interfaces with the Sqlite db and tries to delete a row. The problem is that both qDebug statements give the same number for rowCount. So you remove a row, but the rowCount doesn't change. This confuses the TableView qml control that I am using in the other program and forces it to show empty rows at the end of the table.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      manfred_exz
      wrote on last edited by
      #2

      As the documents for QSqlTableModel::removeRows(not removeRow) says,

      "Deletions are submitted immediately to the database. The model retains a blank row for successfully deleted row until refreshed with select()."

      QSqlTableModel doesn't reimplement removeRow, but I think it works the same. So you should call select()

      1 Reply Last reply
      3

      • Login

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