Results 1 to 10 of 10

Thread: QDomNode delete children

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2006
    Posts
    81
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: QDomNode delete children

    clear() won't delete the node itself, just make null node of it.

    The code snippet you provided will now remove immediate child nodes, "cell elements" that is. I'd guess in general these cell elements will have child nodes themselves, e.g. text. As I wrote before, you'd have to make sure you traverse all descendant nodes, no matter what sublevel or type.

  2. #2
    Join Date
    Aug 2009
    Posts
    92
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: QDomNode delete children

    Quote Originally Posted by zaphod.b View Post
    clear() won't delete the node itself, just make null node of it.

    The code snippet you provided will now remove immediate child nodes, "cell elements" that is. I'd guess in general these cell elements will have child nodes themselves, e.g. text. As I wrote before, you'd have to make sure you traverse all descendant nodes, no matter what sublevel or type.
    It's exactly the other way round as explained by the Qt support via email, received today.

    When you call removeChild() then all it is going to do is remove it from the current tree, you still get a reference to the QDomNode that you have removed which is why it still exists in memory because it hasn't been actually deleted. If you want to ensure that the contents of the node are also deleted then you need to call clear() on that node. This will cause it to be deleted and point to a null node and therefore the memory should be freed up for you. See:

    http://qt-project.org/doc/qt-4.8/qdomnode.html#clear
    But I will answer them tomorrow because this is not true.
    If I only call clear(), nothing is deleted. The call to clear() seems to have no consequences.
    Or, probably, for clear() it's as you said about removeChild(), call it recursively.

  3. #3
    Join Date
    Jun 2006
    Posts
    81
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: QDomNode delete children

    Sorry for the confusion. I remembered incorrectly from the distant past.

    Meanwhile I had a look at the sources (5.1 but shouldn't have changed).
    As you'll have seen from your debugging, clear() simply calls the impl dtor[*]:
    Qt Code:
    1. void QDomNode::clear()
    2. {
    3. if (impl && !impl->ref.deref())
    4. delete impl;
    5. impl = 0;
    6. }
    To copy to clipboard, switch view to plain text mode 
    The dtor indeed iterates the children, thus traversing the tree[*]:
    Qt Code:
    1. QDomNodePrivate::~QDomNodePrivate()
    2. {
    3. QDomNodePrivate* p = first;
    4. QDomNodePrivate* n;
    5.  
    6. while (p) {
    7. n = p->next;
    8. if (!p->ref.deref())
    9. delete p;
    10. else
    11. p->setNoParent();
    12. p = n;
    13. }
    14. first = 0;
    15. last = 0;
    16. }
    To copy to clipboard, switch view to plain text mode 
    [*] I'm not sure what exactly deref() does, but if I interpret the asm correctly, it simply decrements the ref count and returns false if there are no references left, true otherwise. In the latter case deletion traversal terminates, and the node's subtree will dangle in the sense that it remains owned by the doc but is child of this node no more.

    My conclusion is, if not all of your "row" is deleted, there must be reference(s) to its descendant(s). This is in accordance with the doc cited in post #2.

    If you provided relevant parts of your code there might be a chance to locate the culprit.
    Last edited by zaphod.b; 11th December 2013 at 00:20.

Similar Threads

  1. convert from QDomElement to QDomNode
    By julida in forum Newbie
    Replies: 1
    Last Post: 18th January 2011, 03:18
  2. "new" + "delete" a QWidget and its children
    By vkincaid in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2010, 21:51
  3. Replies: 3
    Last Post: 8th September 2008, 18:18
  4. QDomNode
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 06:11
  5. QDomNode
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 12:18

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.