Important: Please read the Qt Code of Conduct -

[SOLVED] QDomNode is always null

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

    if (item == NULL)
     return QDomNode();    
    QDomNode node;
    // 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)
     QString sValue = columnItem->text();
     if (sValue.isEmpty())
     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())
    return node;

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

  • Moderators

    because you use lines like these:

    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 ...

  • 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

  • Thanks, phj_. Now it works fine.

Log in to reply