Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDomNode::isNull() and QDomNode::hasChildNodes()
Forum Updated to NodeBB v4.3 + New Features

QDomNode::isNull() and QDomNode::hasChildNodes()

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on last edited by
    #1

    While refactoring some code, I came across the following if-clause several times:

    const QDomElement foo = mfGetSomeQDomElementFromSomewhere();
    if (!foo.isNull() && foo.hasChildNodes())
    { ... do stuff ...}
    

    I am wondering why the author added the check !foo.isNull()... I am thinking of refactoring this to

    if (foo.hasChildNodes()) {...}
    

    because if foo has child nodes, it implies that it is not null, right?

    Just wanted to check my reasoning and that I haven't overlooked something. Feel free to confirm or point me to mistakes in my reasoning :-)

    kshegunovK 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Not checked with QDomElement but
      such check can also be that
      if foo.isNull() then its not safe to to call foo.hasChildNodes()
      But purely speculating.

      1 Reply Last reply
      0
      • B Bart_Vandewoestyne

        While refactoring some code, I came across the following if-clause several times:

        const QDomElement foo = mfGetSomeQDomElementFromSomewhere();
        if (!foo.isNull() && foo.hasChildNodes())
        { ... do stuff ...}
        

        I am wondering why the author added the check !foo.isNull()... I am thinking of refactoring this to

        if (foo.hasChildNodes()) {...}
        

        because if foo has child nodes, it implies that it is not null, right?

        Just wanted to check my reasoning and that I haven't overlooked something. Feel free to confirm or point me to mistakes in my reasoning :-)

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Bart_Vandewoestyne said in QDomNode::isNull() and QDomNode::hasChildNodes():

        because if foo has child nodes, it implies that it is not null, right?

        Yes, but not having child nodes doesn't mean the node is null. ;)

        Just wanted to check my reasoning and that I haven't overlooked something.

        I don't think you have overlooked anything, it should be safe to remove the first part of the check.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bart_Vandewoestyne
          wrote on last edited by
          #4

          We are still using Qt 4.8.7, so I checked the source:

          bool QDomNode::hasChildNodes() const
          {
              if (!impl)
                  return false;
              return IMPL->first != 0;
          }
          
          bool QDomNode::isNull() const
          {
              return (impl == 0);
          }
          

          so from this I conclude that my assumption was right and the refactoring was ok :-)

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved