Results 1 to 13 of 13

Thread: Deleting a child widget

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Deleting a child widget

    Just to give you an example of needing to call delete on a Qt class instance - if you remove an item from a QGraphicsScene then you have ownership and will need to delete it manually.
    Yes, agreed. I think of this as a special case, though. Since QGraphicsScene contents are often dynamic, there needs to be special handling of item addition and removal. QWidget hierarchies are usually more static. But if you remove an item from a scene and that item itself has child items, when you delete the item, its children will automatically be deleted by the parent-child ownership protocol.

    Constructed "top"
    Constructed "a"
    Constructed "b"
    Constructed "c child of a"
    Destroyed "top"
    Destroyed "a"
    Destroyed "c child of a"
    Destroyed "b"
    This is interesting - I thought "top" would have been shown as being destroyed last. My understanding of C++ construction was that construction proceeded from most-derived up to base class, and destruction proceeded from base class downward to most-derived, so the body of the destructor for the most-derived class would be executed last. Thus I would have expected that the messages for destruction would show "c", then "a", "b", and finally "top".

    Obviously my understanding must be faulty, or is this behaviour an artifact of qDebug()?

    Edit ---

    My understanding of C++ construction was that construction proceeded from most-derived up to base class, and destruction proceeded from base class downward to most-derived,
    Duh (after performing a head-whack) - no this is completely backward; construction goes up the hierarchy, with the body of the base class executed first, followed by each derived class, until finally the most-derived class's constructor gets executed. Destruction goes the other way. So the qDebug() statements as originally given do show things happening in the order they actually occur. My apologies for not having had enough coffee before starting to type.
    Last edited by d_stranz; 14th December 2012 at 19:50.

Similar Threads

  1. Deleting characters in QLineEdit widget
    By Santiago in forum Newbie
    Replies: 2
    Last Post: 11th November 2011, 15:18
  2. Deleting children of a widget
    By ComServant in forum Newbie
    Replies: 5
    Last Post: 7th October 2011, 01:09
  3. deleting a widget
    By john_god in forum General Programming
    Replies: 1
    Last Post: 30th December 2010, 10:36
  4. Replies: 7
    Last Post: 19th May 2010, 14:24
  5. Replies: 1
    Last Post: 28th July 2006, 15:10

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.