vinayan
vinayan

Reputation: 1637

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 )
{
    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?

Upvotes: 52

Views: 92237

Answers (13)

Abhishek Malviya
Abhishek Malviya

Reputation: 1

    table->clear();
    table->setRowCount(0);
    table->setColumnCount(0);
    table->horizontalHeader()->reset();
    table->setHorizontalHeaderLabels(hlables);

This Works Great for Me!!

Upvotes: -2

Mahmudur Rahman Shovon
Mahmudur Rahman Shovon

Reputation: 494

In python you can just set the rowCount to zero and that'll work!

tableWidget.setRowCount(0)

This code is tested with PySide6. Hope it will work for PyQt5 and PyQt6 too.

Upvotes: 1

Rishabh Gupta
Rishabh Gupta

Reputation: 87

This works for me:

for i in reversed(range(self.tableWidget.rowCount())):
    self.tableWidget.removeRow(i)

Upvotes: 0

damkrat
damkrat

Reputation: 364

AFAIK setRowCount(0) removes nothing. Objects are still there, but no more visible.

yourtable->model()->removeRows(0, yourtable->rowCount());

Upvotes: 14

Alex
Alex

Reputation: 1295

You can just add empty item model (QStandardItemModel) to your QTableView (myTableView):

itemModel = new QStandardItemModel;
ui->myTableView->setModel(itemModel);

Upvotes: 1

Jens A. Koch
Jens A. Koch

Reputation: 41756

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

Upvotes: 7

d.danailov
d.danailov

Reputation: 9800

Look this post : http://forum.qt.io/topic/1715/qtablewidget-how-to-delete-a-row

QList<QTableWidgetItem*> items = table.findItems(.....);
QMap<int, int> rowsMap;
for(int i = 0; i < items.count(); i++{
  rowsMap[items.at(i).row()] = -1; //garbage value
}
QList<int> rowsList = rowsMap.uniqueKeys();
qSort(rowsList);

//Now go through your table and delete rows in descending order as content would shift up and hence cannot do it in ascending order with ease.
for(int i = rowList.count() - 1; i >= 0; i--){
  table.removeRow(rowList.at(i));
}

Upvotes: 1

R&#233;mi Debord
R&#233;mi Debord

Reputation: 41

In order to prevent an app crash, disconnect all signals from the QTableView.

// Deselects all selected items
ui->tableWidget->clearSelection();

// Disconnect all signals from table widget ! important !
ui->tableWidget->disconnect();

// Remove all items
ui->tableWidget->clearContents();

// Set row count to 0 (remove rows)
ui->tableWidget->setRowCount(0);

Upvotes: 4

user2076767
user2076767

Reputation: 1

Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same.

void QTableWidget::clearContents()

Removes all items in the view. This will also remove all selections and headers.

void QTableWidget::clear()

Upvotes: 0

Alex Gurkin
Alex Gurkin

Reputation: 129

QTableWidget test;
test.clear();
test.setRowCount( 0);

Upvotes: 10

alexisdm
alexisdm

Reputation: 29886

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);
}

Upvotes: 116

AB Bolim
AB Bolim

Reputation: 2115

Your code does not delete last row.

Try this one.

int totalRow = mTestTable->rowCount();
for ( int i = 0; i < totalRow ; ++i )
{
       mTestTable->removeRow(i);
}

In your code, on the first time, rowCount() have value 2 and value of the i is 0, so its delete 1st row,

But on the second time value of i incremented with 1, but rowCount() return the updated row count which is now 1, so, it does not delete the last row.

Hope now you ll be clear.

Upvotes: 0

john
john

Reputation: 87959

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);
}

Upvotes: 30

Related Questions