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. [SOLVED] QDomNode is always null
Qt 6.11 is out! See what's new in the release blog

[SOLVED] QDomNode is always null

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.3k Views 1 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.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    I wrote some method which returns QDomNode :
    @
    private: QDomNode createNode(QStandardItem * item) {

    if (item == NULL)
     return QDomNode();    
    
    QDomNode node;
    node.toElement().setTagName(item->text());
    
    // write attributes
    QStandardItem * parent = item->parent();
    
    if (parent == NULL)
     parent = invisibleRootItem();
    
    for (int i = 1; i < columnCount(); i++) {
    
     QStandardItem * columnItem = parent->child(item->row(), i);
    
     if (columnItem == NULL)
      continue;
    
     QString sValue = columnItem->text();
    
     if (sValue.isEmpty())
      continue;
    
     node.toElement().setAttribute(headerData(i,Qt::Horizontal,Qt::DisplayRole).toString(), sValue);     
    }
    
    int childCount = item->rowCount();
    
    for (int i = 0; i < childCount; i++) {
    
     QDomNode childNode = createNode(item->child(i));
    
     if (childNode.isNull())
      continue;
    
     node.appendChild(childNode);
    }
    
    return node;
    

    }
    @
    But it always returns a null node, no matter of arguments. What am I doing wrong ?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      because you use lines like these:
      @
      node.toElement()...
      @

      You should create a QDomElement and do all your work directly on it instead a QDomNode and convert it every time.
      Currently you only call your methods on on converted elements (from your intitial node) ... which get discarded right in the next line ...

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pkj__
        wrote on last edited by
        #3

        You are not creating a QDomNode using QDomDocument::createElement() or similar functions. Since the stack variable QDomNode node is null, toElement() also returns a null. Create a valid node element first using QDomDocument variable for which the node is to be created.
        @ QDomDocument doc;
        QDomElement elem = doc.createElement("element"); @
        Pass the doc element or create a new one as required. Refer QDomDocument and QDomNode and QDomElement documentation

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anticross
          wrote on last edited by
          #4

          Thanks, phj_. Now it works fine.

          1 Reply Last reply
          0

          • Login

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