PDA

View Full Version : Question about Removing multiple rows from QTreeWidget



yren
5th March 2010, 17:16
Guys,

I have a QTreeWidget holding a constantly updating view display. When the item list grows too big, I want to remove some of them. The example code is as following:

// remove the top 10 rows
for (int i = 0; i < 10; i++)
delete treeWidget->takeTopLevelItem(0);

// append a new row
treeWidget->addTopLevelItem(new someItem);

The intension of this code is to remove the top 10 items from the view, the add a new row. I figured that after remove the first row, the view re-arranges itself so that the second row becomes the first row, and so on. But, the reality is different: except the first row, other 9 removed rows were not from the top 10 rows, they are random! They could be from the rows I do not intend to remove! The worst is, the view is not consolidated after the deletion, and left the empty rows in it.

My question: how do I properly remove multiple rows from a QTreeWidget? I programed Windows MFC for more than 15 years, I probably have my logic following the MFC thinking all the way. QT might be different.


Thanks!
YR

vcp
5th March 2010, 17:39
Hi

try this code or adapts for you needs:



QList<QTableWidgetItem *> items = tableWidget->selectedItems();
if (!items.isEmpty()) {
foreach (QTableWidgetItem *item, items)
delete item;
}

yren
5th March 2010, 18:45
Thanks! It still doesn't work. According to your suggestion, I collect the top 10 items into a list, then run the deletion. I crashed the application. I think the problem is, after the deletion/removal, I then append a new row to the view. That upsets QT. Somehow, it lost the synchronization. I still struggle to understand how QT deals with the removal. If I remove the top row, would QT reconcile the view and re-index every existing rows? If this is the case, after deleting the row 0, then row 1 becomes row 0, so deleting row 0 five times is equivalent to deleting the top 5 rows. This should be very straightforward!

yren
5th March 2010, 18:58
And, if I just delete only ONE row, it is pretty happy.

yren
6th March 2010, 01:10
I forgot to mention that I call for the deletion in a sub-thread, while the tree widget is in the main GUI thread. I also did some experiment to do the same thing in the main thread, and it works. I guess I'm missing something here. Obviously, there is a difference between doing it from within main and sub-thread in the QT.

yren
8th March 2010, 17:51
I just fix it by designing a pair of signal/slot to update the QTreeWidget in main GUI thread. I still think it should be a bug in QT. In MFC, I do not have any problem to update the main GUI no matter which thread I do it within. At the end, it is just a function call through a defined pointer.:mad: