Results 1 to 6 of 6

Thread: Question about Removing multiple rows from QTreeWidget

  1. #1
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Question about Removing multiple rows from QTreeWidget

    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

  2. #2
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about Removing multiple rows from QTreeWidget

    Hi

    try this code or adapts for you needs:

    Qt Code:
    1. QList<QTableWidgetItem *> items = tableWidget->selectedItems();
    2. if (!items.isEmpty()) {
    3. foreach (QTableWidgetItem *item, items)
    4. delete item;
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about Removing multiple rows from QTreeWidget

    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!

  4. #4
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about Removing multiple rows from QTreeWidget

    And, if I just delete only ONE row, it is pretty happy.

  5. #5
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about Removing multiple rows from QTreeWidget

    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.

  6. #6
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Question about Removing multiple rows from QTreeWidget

    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.

Similar Threads

  1. removing items QTreeWidget
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2015, 21:58
  2. Removing items from qtreeview
    By Babak in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2009, 19:44
  3. removing model Items
    By gyre in forum Newbie
    Replies: 2
    Last Post: 25th November 2007, 20:10
  4. QTreeWidget - scrolls to top when removing items
    By durbrak in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2006, 22:02
  5. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 12:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.