How to delete all rows from QTableWidget
Just set the row count to 0 with:
mTestTable->setRowCount(0);
it will delete the QTableWidgetItem
s 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)
return;
if (rc < rows)
insertRows(qMax(rc, 0), rows - rc);
else
removeRows(qMax(rows, 0), rc - rows);
}
AFAIK setRowCount(0)
removes nothing. Objects are still there, but no more visible.
yourtable->model()->removeRows(0, yourtable->rowCount());
I don't know QTableWidget
but your code seems to have a logic flaw. You are forgetting that as you go round the loop you are decreasing the value of mTestTable->rowCount()
. After you have removed one row, i
will be one and mTestTable->rowCount()
will also be one, so your loop stops.
I would do it like this
while (mTestTable->rowCount() > 0)
{
mTestTable->removeRow(0);
}