Removing row from QTableWidget, showing no effect
-
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?
-
[quote author="KA51O" date="1326361061"]Since the table gets its data from the model I'd say remove them from the model.[/quote]
QTableWidget provides its own, private model, and a separate API to manipulate the items in that model, so this does not work for QTableWidget.
[quote author="aurora" date="1326359238"]
Not just clearing those rows, i want to remove those rows from table....[/quote]
OK, if you just want to delete specific rows, you should indeed use removeRow. Note however that your loop is wrong. Let's work through it step by step:You start with row number NewRow and assing that to m
You remove this row m
Now, row m + 1 moved up in the table, and thus becomes the new row number m
You increment m
You compare m with the rowcount
if m is smaller then rowcount, you continue with step 2
else you exit your loop.
Step 3 is crusial, and probably not what you expected. Because after removing row m, you increment m, you are not going to remove the row that moved to row m after your deletion!
There are two simple ways out of this problem. The general solution for avoiding issues with chaning indexes in lists while iterating over such a list, is to iterate backwards: from the last item forward. If you remove an item while iterating backwards, only indexes of items that you have already iterated over will change, and you don't care about these. In your case, you can also simply not increment m in your loop. That works because you remove all rows after row NewRow, and thus rowCount() will drop to m.