Results 1 to 2 of 2

Thread: Destroying a QList the right way

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Destroying a QList the right way

    Hi!

    My C++ skills are still faint. How do I delete a QList the right way?

    Qt Code:
    1. QList<T> ql = new QList<T>;
    2. ql.append(something);
    3. ql.clear()
    To copy to clipboard, switch view to plain text mode 

    Does clear() call the destructors on the objects in the list? (If they are objects that is, and not pointers or primitives.)

    Or do I have to it like this?

    Qt Code:
    1. QList<T> ql = new QList<T>;
    2. ql.append(something);
    3. for (int i = 0; i < ql.size(); i++)
    4. delete ql.at(i);
    5. ql.clear()
    To copy to clipboard, switch view to plain text mode 

    What would happen if I call delete[] ql?


    Thanks
    Cruz

  2. #2
    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: Destroying a QList the right way

    look at qDeleteAll if you use pointers in QList, if not then QList::clear enough.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    Cruz (19th January 2009)

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. Q3Ptrlist v/s QList
    By joseph in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2007, 11:27
  4. QList
    By dragon in forum Qt Programming
    Replies: 11
    Last Post: 9th May 2007, 20:15
  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.