Nominate our 2022 Qt Champions!

Removing rows from TableModel

  • I'm currently learning Qt and I've found a Qt Address Book Example.

    I would like to change some features but I've been struggling with the TableModel class.
    This method used to work with a user interaction, the user clicks on a contact and then clicks the "Delete contact" button to delete the contact.

    I'd like to implement instead a method that simply deletes all the entries in my TableModel table.


    #ifndef IPWIDGET_H
    #define IPWIDGET_H
    #include "TableModel.h"
    #include <QItemSelection>
    #include <QTabWidget>
    class QSortFilterProxyModel;
    class QItemSelectionModel;
    class IpWidget : public QTabWidget
        IpWidget(QWidget *parent = 0);
        void addEntry(QString name, QString address);
        void removeAllEntries();
        void selectionChanged (const QItemSelection &selected);
        void setupTabs();
        TableModel *table;
        QSortFilterProxyModel *proxyModel;
    #endif // IPWIDGET_H


    void IpWidget::addEntry(QString name, QString ip)
        QList<QPair<QString, QString>> list = table->getList();
        QPair<QString, QString> pair(name, ip);
        table->insertRows(0, 1, QModelIndex());
        QModelIndex index = table->index(0, 0, QModelIndex());
        table->setData(index, name, Qt::EditRole);
        index = table->index(0, 1, QModelIndex());
        table->setData(index, ip, Qt::EditRole);
    void IpWidget::removeEntry()
        QTableView *temp = static_cast<QTableView*>(currentWidget());
        QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
        QItemSelectionModel *selectionModel = temp->selectionModel();
        QModelIndexList indexes = selectionModel->selectedRows();
        foreach (QModelIndex index, indexes) {
            int row = proxy->mapToSource(index).row();
            table->removeRows(row, 1, QModelIndex());
    void IpWidget::removeAllEntries()
        table->removeRows(0, table->rowCount(QModelIndex()), QModelIndex());

    I wrote this, but I'm not sure it's correct, could you guys clarify me this class ?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Just to be sure I understand your question correctly, are you asking us to review your code and explain it to you ?

  • @SGaist I said this code was coming from Qt Address Book Example. I've tried it, but I'd like to change some features.
    By the way, i'm not sure to understand how the TableModel works. I tried to clear it but

    table->removeRows(0, table->rowCount(QModelIndex()));

    doesn't work. So yes, can you clarify me this example ?

  • @theo_ld

    table->removeRows(0, table->rowCount(QModelIndex()));

    doesn't work. So yes, can you clarify me this example ?

    What does "doesn't work" mean? Compile error? Runtime error? Not the behaviour you expected? ...

    1. What is your table->editStrategy()? Docs state that only OnManualSubmit allows mutliple-row deletion. And then you'll need a submitAll().
    2. Check the return result of your table->removeRows(), and lastError() if that's false.
    3. I think table->clear() is a quick way of deleting all rows, though that may not affect what you are wanting to learn about.

  • I run through a Runtime Error ! I'm trying to fix it with your advice ! I believe the


    method doesn't exist in TableMethod btw ;) Can you use


    it if the table is empty ?

  • @theo_ld

    So tell us the runtime error! And/or check the return result + lastError()!

    clear method: what is your TableModel derived from?

  • @JNBarchan

    My TableModel comes from QAbstractTableModel.
    lastError() works only for SQL Databases right ? Which object is it stored in ?
    Can't check the return of the function because I get a runtime error.

    Here is the console output

    Debugging starts
    ASSERT: "last >= first" in file itemmodels\qabstractitemmodel.cpp, line 2743
    Debugging has finished

  • @theo_ld
    Yeah, I had assumed QSqlTableModel....

    ASSERT: "last >= first" in file itemmodels\qabstractitemmodel.cpp, line 2743

    I would guess this is telling you that your start row number + number of rows >= last row in table.

    So: look at value of table->rowCount(QModelIndex()). Is it 0? In which case, if there are 0 rows, no, you cannot delete any rows from an empty table, even if you specify a count of 0 rows to delete. At a guess!

    Also, QAbstractTableModel requires you to subclass (TableModel), and unless that has implemented removeRows() is does nothing? You sure the example which did work did not use removeRow() (note the spelling)?

  • @JNBarchan You might be right, so I added a condition and it doesn't runtime anymore.

    Although, my user isn't visible at all in the graphical interface -_-.
    Trying to fix this problem now :p

  • @theo_ld said in Removing rows from TableModel:

    @JNBarchan You might be right, so I added a condition and it doesn't runtime anymore.

    In that case, I'd suggest I was right... ;-)

  • This post is deleted!

Log in to reply