Results 1 to 5 of 5

Thread: Procorrect way to remove checked elements from a QListWidget

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Procorrect way to remove checked elements from a QListWidget

    Hi to all,
    this seems to be a simple task but is error prone.
    I have a QListWidget where I insert some items that have the chequeable flag. At some point I need to remove ( delete ) from the list only the checked items.
    is not possible to use a for loop due to the fact that the list size change every time an item is removed. I also tried using qDeleteAll and the findItems but how to crete a pattern with only checked items?
    Thanx in advance,
    Franco
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Procorrect way to remove checked elements from a QListWidget

    Hi,

    You have to do a "while" loop until no deletion needs to be performed. Into loop you have to search the first item to be deleted. When no item needs to be deleted you can exit the loop.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Procorrect way to remove checked elements from a QListWidget

    Hi,
    thanx. It can be possible to have a small example or preudo code?
    Thanx
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Procorrect way to remove checked elements from a QListWidget

    Hi,

    I hope something like this will work:

    Qt Code:
    1. bool bDeletion = true;
    2. while (bDeletion)
    3. {
    4. //Search element to delete. We have to get the number of elements every start of searching
    5. int i=0;
    6. bool bFound = false;
    7. int iNumElements = m_qListWidget.count();
    8. while (!bFound && (i<iNumElements))
    9. {
    10. if (m_qListWidget.at(i)->elementHasToBeDeleted()) //Implement your method
    11. bFound = true; // "i" points to element to delete
    12. else
    13. i++; //Jump to next element
    14. }
    15.  
    16. //If we have found one element to delete, just delete it
    17. if (bFound)
    18. delete m_qListWidget.takeAt(i);
    19. //If we have not found any element to delete just exit the loop (job finished)
    20. else
    21. bDeletion = false;
    22. }
    To copy to clipboard, switch view to plain text mode 

    As I supose that no many items are there on the list, the search of an item to be deleted starts every time from item 0. You could save the deletion position and start the search againg from this value but you have to get the number of items again and check that it is not out of bounds.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Procorrect way to remove checked elements from a QListWidget

    It couldn't be easier :
    Qt Code:
    1. int i=0;
    2. int iNumElements = m_qListWidget.count();
    3. while (i<iNumElements)
    4. {
    5. if (m_qListWidget.at(i)->elementHasToBeDeleted()) //Implement your method
    6. {
    7. delete m_qListWidget.takeAt(i);
    8. iNumElements--;//Because we have removed one item from the list
    9. }
    10. else
    11. {
    12. i++; //Jump to next element
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 22nd February 2019, 17:18
  2. Replies: 0
    Last Post: 16th February 2011, 10:23
  3. Replies: 3
    Last Post: 25th July 2008, 15:30
  4. Remove first n elements from QList
    By pakulo in forum Qt Programming
    Replies: 8
    Last Post: 4th June 2007, 08:27
  5. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 09:58

Tags for this Thread

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.