Results 1 to 2 of 2

Thread: What happens when i call clear() of QList<QObject *> variable?

  1. #1
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    9

    Default What happens when i call clear() of QList<QObject *> variable?

    I have the following code:

    Qt Code:
    1. QList<QObject *> m_devices;
    2. m_devices.append(new Data::Device(info));
    To copy to clipboard, switch view to plain text mode 

    When do I call the method m_devices.clear() from QList, the allocated memory (new Data::Device(info)) will be free correctly or I need do delete one by one before?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What happens when i call clear() of QList<QObject *> variable?

    If "info" is a QObject and is assigned as parent to Data:evice objects, then memory will be freed after "info" object is deleted.
    Otherwise you have to delete items manually, for example:
    Qt Code:
    1. qDeleteAll(m_devices);
    To copy to clipboard, switch view to plain text mode 
    clear() will only remove the pointers from memory, but will not call "delete" on them.

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

    ricardodovalle (14th March 2014)

Similar Threads

  1. clear qlist inside qmap
    By franki in forum Newbie
    Replies: 3
    Last Post: 16th December 2013, 14:17
  2. Replies: 0
    Last Post: 27th September 2013, 07:09
  3. QList < QHash < int, QString> > ... clear() is slow
    By JoZCaVaLLo in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2011, 12:07
  4. call variable from other function
    By walito in forum Newbie
    Replies: 7
    Last Post: 31st August 2007, 19:36
  5. Fastest way to clear a QList
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 10:26

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.