vinayan vinayan - 5 months ago 70
C++ Question

How to delete all rows from QTableWidget

I am trying to delete all rows from a QTableWidget . Here is what I tried.

for ( int i = 0; i < mTestTable->rowCount(); ++i )

I had two rows in my table. But this just deleted a single row. A reason could be that I did not create the the table with a fixed table size. The Qt Documentation for rowCount() says,

This property holds the number of rows in the table.

By default, for a table constructed without row and column counts,
this property contains a value of 0.

So if that is the case, what is the best way to remove all rows from table?


Just set the row count to 0 with:


it will delete the QTableWidgetItems automatically, by calling removeRows as you can see in QTableWidget internal model code:

void QTableModel::setRowCount(int rows)
    int rc = verticalHeaderItems.count();
    if (rows < 0 || rc == rows)
    if (rc < rows)
        insertRows(qMax(rc, 0), rows - rc);
        removeRows(qMax(rows, 0), rc - rows);