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. How to remove row from QsqlTableModel?
Forum Update on Monday, May 27th 2025

How to remove row from QsqlTableModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.6k 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by Infinity
    #1

    I have the following code:

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
        m_ui(new Ui::MainWindow),
        m_model(new QSqlTableModel(this))
    {
        m_ui->setupUi(this);
        m_model->setTable("test");
        m_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        m_model->select();
    
        m_ui->tableView->setModel(m_model);
    
        qDebug() << "Select       : Row count:" << m_model->rowCount();
    
        connect(m_ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
                this, &MainWindow::on_selectionChanged);
    }
    
    MainWindow::~MainWindow()
    {
        delete m_ui;
    }
    void MainWindow::on_pushButtonNewRecord_clicked()
    {
        qDebug() << "New Record";
    
        m_record = m_model->record();
        m_record.setValue("firstname", "john");
        m_record.setValue("lastname", "doe");
        m_record.setValue("email", "john.doe@email.com");
    
        m_model->insertRecord(-1, m_record);
    
        qDebug() << "New Record   : Row count:" << m_model->rowCount();
    }
    
    void MainWindow::on_pushButtonRemoveRow_clicked()
    {
        qDebug() << "Remove Row";
    
        if (m_row >= 0) {
            m_model->removeRows(m_row, 1);
    
            qDebug() << "Remove Record: Row count:" << m_model->rowCount();
        }
    }
    
    void MainWindow::on_pushButtonSubmit_clicked()
    {
        qDebug() << "Submit";
    
        m_model->submitAll();
    }
    
    
    void MainWindow::on_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
    {
        Q_UNUSED(deselected)
    
        if (!selected.isEmpty()) {
            m_row = selected.indexes().first().row();
        }
    }
    

    I can add new records with insertRecord and remove records with removeRows without any problem, but when I read the records from the database with select and want to remove one of these records, this doesn't work. Even if I call removeRows on them, they are not removed from the model. They will be removed from the model when I call submitAll. How can I achieve that also the selected rows are removed from the model when I call removeRows, without calling submitAll?

    LematL 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I would check for the return value of removeRow() and QSqlQueryModel::lastError()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You have specified the edit strategy to be on manual submit. If you want to have things done automatically then change the edit strategy.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        I 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          You have specified the edit strategy to be on manual submit. If you want to have things done automatically then change the edit strategy.

          I Offline
          I Offline
          Infinity
          wrote on last edited by Infinity
          #4

          @SGaist I don't want to have them automatically done. I would like that the user can add and remove records and that they are written to the database only when submit all is called. The user should be able to add or remove records, but if he is not happy he should be able to cancel the action without writing the values to the database. Is there a way to achieve this?

          Records which are inserted with insertRecord will be removed from the model if removeRows is called. But records which were read from the database will not be removed from the model, when removeRows is called. Is there a way that these records are also remove from the model when removeRows is called or at least they should not be showed in the tableView. When submitAll is called the new records should be written to the database and the records which were removed should be deleted in the database.

          Is there a way to find out which rows of the model were removed but are still in the model? Something like a flag? And that only the rows which are not delete are shown in the tableView.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Infinity said in How to remove row from QsqlTableModel?:

            Is there a way to find out which rows of the model were removed but are still in the model?

            Did you do what I wrote?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            I 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Infinity said in How to remove row from QsqlTableModel?:

              Is there a way to find out which rows of the model were removed but are still in the model?

              Did you do what I wrote?

              I Offline
              I Offline
              Infinity
              wrote on last edited by
              #6

              @Christian-Ehrlicher Yes. It returns always true.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                So when lastError() did not return anything I would expect it was removed from the table. Please provide a minimal, compilable example.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Infinity
                  wrote on last edited by Infinity
                  #8

                  If I call removeRows on records which were read from the database a ! will be displayed in the first column of the tableView.

                  e9f0bd54-4d4c-4cae-b053-4ea92dc6b214-image.png

                  The only thing which I would like to achieve is that this row will not be displayed in the view. I tried it with QSortFilterProxyModel but I don't know where I can get the flag which is used to display the ! in the first column. Is there a way to set a filter in QSortFilterProxyModel that it only contains the rows which don't have this flag?

                  From where does the view take the information, that the removed row is marked with a "!" ? This information might be hidden somewhere in the model, but I cannot find out where.

                  1 Reply Last reply
                  0
                  • I Infinity

                    I have the following code:

                    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
                        m_ui(new Ui::MainWindow),
                        m_model(new QSqlTableModel(this))
                    {
                        m_ui->setupUi(this);
                        m_model->setTable("test");
                        m_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
                        m_model->select();
                    
                        m_ui->tableView->setModel(m_model);
                    
                        qDebug() << "Select       : Row count:" << m_model->rowCount();
                    
                        connect(m_ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
                                this, &MainWindow::on_selectionChanged);
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete m_ui;
                    }
                    void MainWindow::on_pushButtonNewRecord_clicked()
                    {
                        qDebug() << "New Record";
                    
                        m_record = m_model->record();
                        m_record.setValue("firstname", "john");
                        m_record.setValue("lastname", "doe");
                        m_record.setValue("email", "john.doe@email.com");
                    
                        m_model->insertRecord(-1, m_record);
                    
                        qDebug() << "New Record   : Row count:" << m_model->rowCount();
                    }
                    
                    void MainWindow::on_pushButtonRemoveRow_clicked()
                    {
                        qDebug() << "Remove Row";
                    
                        if (m_row >= 0) {
                            m_model->removeRows(m_row, 1);
                    
                            qDebug() << "Remove Record: Row count:" << m_model->rowCount();
                        }
                    }
                    
                    void MainWindow::on_pushButtonSubmit_clicked()
                    {
                        qDebug() << "Submit";
                    
                        m_model->submitAll();
                    }
                    
                    
                    void MainWindow::on_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
                    {
                        Q_UNUSED(deselected)
                    
                        if (!selected.isEmpty()) {
                            m_row = selected.indexes().first().row();
                        }
                    }
                    

                    I can add new records with insertRecord and remove records with removeRows without any problem, but when I read the records from the database with select and want to remove one of these records, this doesn't work. Even if I call removeRows on them, they are not removed from the model. They will be removed from the model when I call submitAll. How can I achieve that also the selected rows are removed from the model when I call removeRows, without calling submitAll?

                    LematL Offline
                    LematL Offline
                    Lemat
                    wrote on last edited by Lemat
                    #9

                    @Infinity
                    have you tried out:

                    //m_model->submitAll();
                    m_model->select(); 
                    
                    //remember that your edit strategy is OnManualSubmit
                    

                    in your removeRow slot ?

                    Time to elevate.

                    I 1 Reply Last reply
                    0
                    • LematL Lemat

                      @Infinity
                      have you tried out:

                      //m_model->submitAll();
                      m_model->select(); 
                      
                      //remember that your edit strategy is OnManualSubmit
                      

                      in your removeRow slot ?

                      I Offline
                      I Offline
                      Infinity
                      wrote on last edited by
                      #10

                      @Lemat Yes. Thank you very much. I tried that, but that is not what I'm looking for.

                      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