PDA

View Full Version : removing row from QTableWidget, showing no effect



aurora
12th January 2012, 08:15
Hello all,
I'm trying to remove item from QTable widget as shown below....


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?

aurora
12th January 2012, 11:33
Its ok...
This problem is solved...!

SIFE
12th January 2012, 16:27
How did you solve it?

ChrisW67
12th January 2012, 21:36
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:

#include <QtCore>
#include <QDebug>

int main(int argc, char **argv) {
QCoreApplication app(argc, 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.

wysota
13th January 2012, 00:45
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:

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

aurora
13th January 2012, 04:06
I used backword loop, removing items from last,
As shown below


int m=ui->tableWidget_ouput->rowCount();
while(m>=NewRow)
{
ui->tableWidget_ouput->removeRow(m);
m--;
}

Lykurg
13th January 2012, 07:01
Ehm, what about QTableWidget::clearContents() or QTableWidget::clear()?

EDIT: Forget it, read too fast, didn't see NewRow...

ChrisW67
13th January 2012, 07:10
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.

wysota
13th January 2012, 10:08
Wouldn't this be the quickest?


ui->tableWidget->setRowCount(NewRow);

I don't know what happens with those excessive items, if they are deleted or not.

Lykurg
13th January 2012, 10:43
Because I was curious:

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!