Results 1 to 5 of 5

Thread: deleting all elements in a qlist

  1. #1
    Join Date
    Mar 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question deleting all elements in a qlist

    hello,

    can anyone help me out in deleting all the elements in a qlist??

    i want to clear a qlist totally so that new data can be reloaded...

  2. #2
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: deleting all elements in a qlist

    use clear(). If you need to remove individual item use removeAt()
    clear() will delete all the item.

  3. #3
    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: deleting all elements in a qlist

    Hi,

    If you have pointers stored in the list you have to delete them manually:

    Qt Code:
    1. int iNum = qList.count();
    2. for (int i=0; i<iNumElements; i++)
    3. delete (qList.takeAt(0)); //Always delete element 0
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: deleting all elements in a qlist

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    If you have pointers stored in the list you have to delete them manually:

    Qt Code:
    1. int iNum = qList.count();
    2. for (int i=0; i<iNumElements; i++)
    3. delete (qList.takeAt(0)); //Always delete element 0
    To copy to clipboard, switch view to plain text mode 
    of course if you use pointer as container type, but in this case I would use qDeleteAll insted of this loop.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: deleting all elements in a qlist

    Quote Originally Posted by navi1084 View Post
    clear() will delete all the item.
    No, it won't. It will only destroy pointers, not the objects behind them (same with removeAt() of course). Delete needs to be called explicitely on each item or, as already said, qDeleteAll() can be used.

    Of course all that assumed one "deletes" objects behind pointers. If you have a list of objects then delete is not required as the destructor will be ran when using clear() or removeAt() (but I wouldn't call that "deleting" thus the whole post of mine).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. QList, copy problems
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 5th February 2010, 00:06
  3. Destroying a QList the right way
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 19th January 2009, 10:52
  4. Remove first n elements from QList
    By pakulo in forum Qt Programming
    Replies: 8
    Last Post: 4th June 2007, 07:27
  5. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43

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.