removing row from QTableWidget, showing no effect
Hello all,
I'm trying to remove item from QTable widget as shown below....
Code:
for(int m=NewRow;m<ui->tableWidget_ouput->rowCount();m++)
{
ui->tableWidget_ouput->removeRow(m);
}
But its effect is not displaying i.e, the removed rows still present in the table....Please tell me , what i'm missing here?
Re: removing row from QTableWidget, showing no effect
Its ok...
This problem is solved...!
Re: removing row from QTableWidget, showing no effect
Re: removing row from QTableWidget, showing no effect
When you remove row x then old row x + 1 becomes the new row x. If you then increment x, as happens in the for loop, you skip a row.
You can see the behaviour in the following example:
Code:
#include <QtCore>
#include <QDebug>
int main(int argc, char **argv) {
QList<int> l;
l << 11 << 12 << 13 << 14 << 15 << 16 << 17 << 18 << 19;
qDebug() << "Start" << l;
for (int r = 2; r < l.size(); ++r) {
qDebug() << "r =" << r << "removing" << l.at(r);
l.removeAt(r);
qDebug() << "Remaining" << l;
}
return 0;
}
Either remove the rows in reverse order, or determine the number of rows to remove and remove the first row that number of times. If you are trying to empty an entire QList use clear(). If you are trying to empty an entire QTableWidget set the row count to zero.
Re: removing row from QTableWidget, showing no effect
Quote:
Originally Posted by
ChrisW67
When you remove row x then old row x + 1 becomes the new row x. If you then increment x, as happens in the for loop, you skip a row.
Moreover, removing a row decreases the rowCount() of the table which is what your stop condition depends on:
Code:
for(int m=NewRow;m<ui->tableWidget_ouput->rowCount();m++)
thus you can effectively remove only half of the rows (so every 2nd of the first 75% of the rows in your case).
Re: removing row from QTableWidget, showing no effect
I used backword loop, removing items from last,
As shown below
Code:
int m=ui->tableWidget_ouput->rowCount();
while(m>=NewRow)
{
ui->tableWidget_ouput->removeRow(m);
m--;
}
Re: removing row from QTableWidget, showing no effect
Ehm, what about QTableWidget::clearContents() or QTableWidget::clear()?
EDIT: Forget it, read too fast, didn't see NewRow...
Re: removing row from QTableWidget, showing no effect
I went there too. Those functions remove the content from the table but leave the dimensions the same, i.e. rowCount() is unchanged. Contrast that with removeRow(), as used in the original post, which actually shrinks the table. I guess it really depends on what your application actually requires.
Re: removing row from QTableWidget, showing no effect
Wouldn't this be the quickest?
Code:
ui->tableWidget->setRowCount(NewRow);
I don't know what happens with those excessive items, if they are deleted or not.
Re: removing row from QTableWidget, showing no effect
Because I was curious:
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);
}
Yes, they are!