How to delete all rows from QTableWidget
Solution 1
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);
}
Solution 2
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);
}
Solution 3
AFAIK setRowCount(0)
removes nothing. Objects are still there, but no more visible.
yourtable->model()->removeRows(0, yourtable->rowCount());
Solution 4
QTableWidget test;
test.clear();
test.setRowCount( 0);
Solution 5
The simple way to delete rows is to set the row count to zero. This uses removeRows() internally.
table->setRowCount(0);
You could also clear the content and then remove all rows.
table->clearContents();
table->model()->removeRows(0, table->rowCount());
Both snippets leave the headers untouched!
If you need to get rid of headers, too, you could switch from clearContents()
to clear()
.
Related videos on Youtube
![vinayan](https://i.stack.imgur.com/FLLPA.jpg?s=256&g=1)
vinayan
a GIS enthusiast, Keenly following open source GIS technologies..
Updated on February 02, 2022Comments
-
vinayan over 2 years
I am trying to delete all rows from a QTableWidget . Here is what I tried.
for ( int i = 0; i < mTestTable->rowCount(); ++i ) { mTestTable->removeRow(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?
-
CodeLurker over 5 yearsThe title of your post is a noun phrase, not a question. It should be "How do you," "How does one", "What is the best way," e.g.
-
-
vinayan about 11 yearsthis does not seem to work..probably the index keeps changing when a row is deleted..
-
vinayan about 11 yearsthis could work..but seems a lot of code to delete a couple of rows :)
-
AB Bolim about 11 years@vinayan - Sorry you are wrong. If you consider the total number of row is 2, and on the 1st iteration
rowCount()
= 2, the 1st row is deleted on the index of(i = 0)
0th, and on the 2nd iterationrowCount()
= 1 and index of(i=1)
1st so its try to delete on index of 1st but in on 1st index row is shifted to 0th index as 0th row is deleted, So here the code posted by @Vinayan wont work.. -
UmNyobe about 11 yearsthis is appropriate only to delete a specific set of rows
-
vinayan about 11 yearsyou are right about the logic..actually the rowCount() is not giving me the the 2 value as is should since Qt gives rowCount() = 0 for tables created without predefined rows as is in my case..thanks for the answer
-
alexisdm almost 11 yearsYour code doesn't work, you can't delete the rows by their index in that order, because deleting a row shift the index of all the rows that follow it (i=0 => next row to delete is still at i=0). If you had to use the index, you could delete the row in the reverse order, so that deleting one index doesn't "invalidate" the other indexes (
for ( int i = totalRow-1; i > 0 ; --i )
). -
sshilovsky about 10 yearsthis is not really good solution as it can be enormously slow on big data.
removeRow(mTestTable->rowCount()-1)
is expected to be much faster. -
skrrgwasme over 9 yearsIt would be good to add some comments or explanations to code in your answers.
-
rhodysurf over 9 yearsThis is the easiest way to do it. also +1 for setRowCount(0)
-
SexyBeast about 9 yearsThis is a rather bad solution, for
QTableWidget
s with huge number of rows this will take of lot of time. -
blented about 9 yearsThis removes nothing, see @damkrat's answer below
-
Alex Gurkin about 9 yearsThis removes all. Maybe @damkrat is right, setRowCount(0) removes nothing, but clear() - removes.Clear() removes all items, but table dimensions stay the same.
-
Alexandro Sánchez about 6 yearsThis contradicts the accepted answer. Which one is the correct one?
-
AAEM almost 6 yearsthe second function valid only in the QTableWidget , am i right ?
-
damkrat almost 6 yearsTry both. In my case with custom models, i've done it this way, as setRowCount didn't clear rows from model. My version of Qt was 4.4.x
-
alexisdm about 4 yearsYou are not supposed to use a custom model with
QTableWidget
, the internal model,QTableModel
is private, and not a part of the Qt API, so you can't properly subclass it. And because ` QTableWidget` usesqobject_cast
before calling everyQTableModel
function without checking that the cast was successful, if your custom model doesn't inherit fromQTableModel
, you are going to break things. SinceQTableWidget::setRowCount
callsQTableModel::setRowCount
which does callQTableModel::removeRows
which itself deletes theQTableWidgetItem
. -
damkrat about 4 yearsQTableModel inherits QAbstractTableModel though, implementing QTableModel in own manner is a feasible option if e.g. some features are needed. My bad, i'm not sure anymore if i've used QTableWidget vs QTableView in first place.
-
CodeLurker over 2 yearsThe reason not to use
mTestTable->clear();
in many cases, is that it will also remove the column and row titles. It also probably resets column and row widths. -
lloydyu24 about 2 yearsBest way to go: Don't use clear(). Use maybe mTestTable->clearContents(). Then mTestTable->setRowCount(0)