How to delete all rows from QTableWidget

73,904

Solution 1

Just set the row count to 0 with:

mTestTable->setRowCount(0);

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)
        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().

Share:
73,904

Related videos on Youtube

vinayan
Author by

vinayan

a GIS enthusiast, Keenly following open source GIS technologies..

Updated on February 02, 2022

Comments

  • vinayan
    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
      CodeLurker over 5 years
      The 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
    vinayan about 11 years
    this does not seem to work..probably the index keeps changing when a row is deleted..
  • vinayan
    vinayan about 11 years
    this could work..but seems a lot of code to delete a couple of rows :)
  • AB Bolim
    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 iteration rowCount() = 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
    UmNyobe about 11 years
    this is appropriate only to delete a specific set of rows
  • vinayan
    vinayan about 11 years
    you 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
    alexisdm almost 11 years
    Your 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
    sshilovsky about 10 years
    this 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
    skrrgwasme over 9 years
    It would be good to add some comments or explanations to code in your answers.
  • rhodysurf
    rhodysurf over 9 years
    This is the easiest way to do it. also +1 for setRowCount(0)
  • SexyBeast
    SexyBeast about 9 years
    This is a rather bad solution, for QTableWidgets with huge number of rows this will take of lot of time.
  • blented
    blented about 9 years
    This removes nothing, see @damkrat's answer below
  • Alex Gurkin
    Alex Gurkin about 9 years
    This 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
    Alexandro Sánchez about 6 years
    This contradicts the accepted answer. Which one is the correct one?
  • AAEM
    AAEM almost 6 years
    the second function valid only in the QTableWidget , am i right ?
  • damkrat
    damkrat almost 6 years
    Try 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
    alexisdm about 4 years
    You 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` uses qobject_cast before calling every QTableModel function without checking that the cast was successful, if your custom model doesn't inherit from QTableModel, you are going to break things. Since QTableWidget::setRowCount calls QTableModel::setRowCount which does call QTableModel::removeRows which itself deletes the QTableWidgetItem.
  • damkrat
    damkrat about 4 years
    QTableModel 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
    CodeLurker over 2 years
    The 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
    lloydyu24 about 2 years
    Best way to go: Don't use clear(). Use maybe mTestTable->clearContents(). Then mTestTable->setRowCount(0)