Results 1 to 6 of 6

Thread: deleting QStringList

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    74

    Default deleting QStringList

    How do you completely delete (and deallocate memory) a QStringList?
    Is emptying a QStringList the same as deleting it?

    Thanks.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: deleting QStringList

    From the docs -
    QStringList inherits from QList<QString>.
    You dont have pointers stored in QStringList,,only objects. So When you clear it, the object must be deleted or whatever. You should not be concerned about it.
    Unless you used something like - QList<QString*>.

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

    timmu (17th December 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    74

    Default Re: deleting QStringList

    Does this mean that the following will delete a QStringList:

    Qt Code:
    1. QStringList myList;
    2. ........
    3. ........
    4. myList = QStringList();
    To copy to clipboard, switch view to plain text mode 

    Can the same logic be used to delete a QTableWidget?

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: deleting QStringList

    Can the same logic be used to delete a QTableWidget?
    No.

    You only "delete" heap allocated objects (i.e. stuff allocated with new).
    Use delete (or deleteLater()).

    Stack-allocated stuff like your QStringList is "destroyed" when it goes out of scope.
    Qt Code:
    1. {
    2. ...
    3. } // xyz is destroyed here automatically
    To copy to clipboard, switch view to plain text mode 


    Heap:
    Qt Code:
    1. QStringList *heap_sl = new QStringList;
    2. ...
    3. delete heap_sl;
    To copy to clipboard, switch view to plain text mode 
    Heap-allocation is slower and more error-prone. So don't use it when stack based allocation is good enough for your needs.

  6. #5
    Join Date
    Oct 2007
    Posts
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Smile Re: deleting QStringList

    The deletion can be done by using

    qDeleteAll(...)

    This will call the delete function on each element inside the specified container.
    Darshan

  7. #6
    Join Date
    Dec 2006
    Posts
    849
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 163 Times in 151 Posts

    Default Re: deleting QStringList

    but please note the difference between
    Qt Code:
    1. QList<QString*>
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. QList<QString>*
    To copy to clipboard, switch view to plain text mode 
    qDeleteAll only makes sense for the former; but also note that a QStringList is probably the better choice anyway.
    Last edited by caduel; 18th December 2009 at 13:37. Reason: typo

Similar Threads

  1. QStringList
    By jaca in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2008, 10:12
  2. QStringList scope problem
    By ht1 in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 19:44
  3. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  4. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48
  5. need help to classify some QStringList
    By patcito in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 21:24

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
  •  
Qt is a trademark of The Qt Company.